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
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/
We can also test whether the file is actual file or symlink as shown below.
# Test symlink
ls -l <file path>
ls -l /etc/nginx/sites-enabled/mydomain.com
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
-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.
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.