Create Or Delete Symbolic Link In Linux

By bhagwatchouhan
Create Or Delete Symbolic Link In Linux

Symbolic Link also called Soft Link symlink is a virtual link to the physical path of a file or directory. We can also say that the symlinks are pointers to another file or directory on the same system or another system. This tutorial explains how to create the Symbolic Link and also provides steps required to delete or remove or unlink an existing Soft Link.

The most common usage of symlinks is to enable or disable configurations for specific software. The commonly used server software using symlinks are Apache and Nginx web servers having sites-available and sites-enabled directories. The actual configuration files are available in the sites-available directory and enabled/disabled by creating/removing the symbolic links in the sites-enabled directory.

We can also use symbolic links to avoid duplicate files having the same content. Removing or deleting a symbolic link can be considered as the actual deletion of the file. The actual file won't be deleted on deleting a symlink.

Create Symbolic Link

We can create a soft link using the command as shown below.

# Create Symbolic Link - Syntax
ln -s [OPTIONS] FILE LINK

# Example
ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com

The above command will create a soft link of the file /etc/nginx/sites-available/mydomain.com at /etc/nginx/sites-enabled/mydomain.com so that the file can also be accessed using the path /etc/nginx/sites-enabled/mydomain.com. in this way, we can create a symbolic link without duplicating the file content. We can also create symbolic links for a directory following the same steps.

We can also test whether the file is actual file or symlink as shown below.

# Test symlink
ls -l <file path>

# Example
ls -l /etc/nginx/sites-enabled/mydomain.com

# Output
lrwxrwxrwx 1 root root 39 Oct 2 15:16 /etc/nginx/sites-enabled/mydomain.com -> /etc/nginx/sites-available/mydomain.com

# Test regular file
ls -l /etc/nginx/sites-available/mydomain.com

# Output
-rw-r--r-- 1 root root 1092 Oct 2 15:23 /etc/nginx/sites-available/mydomain.com

With the above output, we can clearly distinguish whether the file path is a soft link or actual file.

Remove Symbolic Link

We can remove the soft link by simply deleting it like we delete a normal file. We can also use the command unlink to remove a symlink as shown below. Make sure to perform the symbolic link test before deleting it to ensure that you are not deleting the actual file.

# Delete Symbolic Link
sudo rm /etc/nginx/sites-enabled/mydomain.com

# Remove Symbolic Link
sudo unlink /etc/nginx/sites-enabled/mydomain.com

Both the commands delete the symbolic link without deleting the actual file. We can see that we have used the regular command rm to delete the symbolic link.

Summary

We have discussed the importance and usage of the Symbolic Link and also discussed the examples to create and delete it in the last section of this tutorial.

share on :

Profile picture for user bhagwatchouhan
bhagwatchouhan