Symbolic links or Soft links work like pointers to another file. Note that there is only one copy of the actual file on the hard disk and in this way you can save valuable hard disk space by simply creating a link to it. Deleting a symbolic link is the same as removing a real file or directory.
A symbolic link is a link that refers to the name of a file. Its most important advantage is that it can be used to refer to a file that is anywhere, even on a computer on the other side of the world. The symbolic link will still work. However, the biggest disadvantage is that the symbolic link is naturally dependent on the original file. There are some properties of symbolic links
- Links have different inode numbers : every Linux file or directory (from a technical point of view, there’s no real difference between them) has an inode and this inode contains all of the file’s metadata (that is, all the administrative data needed to read a file is stored in its inode)
ls -lcommand shows all links with second column value
1and the link points to original file.
- Link contains the path for original file and not the contents.
- Removing soft link doesn't affect anything but when the original file is removed, the link becomes a dangling link that points to nonexistent file.
rm and unlink commands to remove symbolic link
Symbolic links can be removed with two commands: rm and unlink. You can use any one of the following commands to remove symbolic links.
- rm: is the terminal command to remove each given file including symbolic links. Because a symbolic link is considered as a file on Linux, you can delete it with the rm command.
# rm linkfile
- unlink: deletes a single specified file name including symbolic links.
# unlink linkfile
To remove the symbolic link file, you should list it with
ls -l command as below
# ls -l pac lrwxrwxrwx 1 root root 9 May 18 01:57 pac -> /root/pac
pac -> /root/pac shows the path of the original file which is
/root/pac and the
1 value on the second column indicates that the file is a symbolic link file.
Delete symbolic link file - Example
To see how we can delete symbolic link file, we will first create a soft link a below
# ln -s /root/script /home/papso
To list how soft link looks:
# ls -l script lrwxrwxrwx 1 root root 12 May 18 02:32 script -> /root/script
To delete symbolic link, you can use its relative or absolute path but to limit error, I recommend you to use a relative path.
With absolute path we have
# rm /home/papso/script
or you can do
# unlink /home/papso/script
We can check as below
# ls -l /home/papso/script ls: cannot access script: No such file or directory
With relative path, we first need to move to the symbolic link file directory
# cd /home/papso
Now you can use one of the commands above
# unlink script
and check with
# ls -l script ls: cannot access script: No such file or directory
Be careful when you use unlink command because it can delete regular file. If you write an existing regular filename instead of a symbolic link filename, the regular file will be deleted. See below
# ls -l file1 -rw-r--r-- 1 root root 0 May 18 02:51 file1
You can see that file1 is not a symbolic link. Now we will use unlink command and we will see the result.
# unlink file1
# ls -l file1 ls: cannot access file1: No such file or directory
You see that file1 doesn't exist. It has been deleted
Delete symbolic link directory - Example
To delete a symbolic link directory, we will use the same procedure as above. We will create a soft link:
# ls -ld pac drwxr-xr-x 2 root root 4096 Apr 6 22:54 pac
pac is the folder which will be used for the test.
# ln -s /root/pac /home/papso/test
# ls -l /home/papso/test/pac lrwxrwxrwx 1 root root 9 May 18 03:00 /home/papso/test/pac -> /root/pac
Now to delete the test symbolic link directory, we will use the relative path. We have moved to
# rm pac
# unlink pac
We will check now with absolute path
# ls -l /home/papso/test/pac ls: cannot access /home/papso/test/pac: No such file or directory
unlink command and rm command without
-R option doesn't delete regular directory. The two commands delete symbolic link from directory because it's considered as a file so, when using the rm or unlink command to remove a symbolic link from a directory, make sure you don’t end the target with a
/ character because that will create an error.
$ mkdir dirfoo $ ln -s dirfoo lnfoo
# ls -l lnfoo lrwxrwxrwx 1 root root 6 May 18 03:16 lnfoo -> dirfoo
Now let's try to delete the symbolic link
# rm lnfoo/ rm cannot remove directory ‘lnfoo/’ : Is a directory
/ at the end indicates a directory, the command doesn't work
# unlink lnfoo/ unlink: cannot unlink ‘lnfoo/’: Not a directory
Now let's try without the
/ character at end of the filename
# unlink lnfoo
Now let's check it the file exists
# ls -l lnfoo ls: cannot access lnfoo: No such file or directory
You can see that the symbolic link has been deleted
Basically, a symbolic link makes it easier to find files you need. You can create symbolic links for the files, directories and you can use them to make life easier for users as well. Symbolic link is considered as a normal file because you can delete it with the basic rm command. Don't forget the biggest disadvantage which imply that the symbolic link is naturally dependent on the original file.