The symbolic link, also known as soft link or symlink, in Linux is a special type of file that works as a shortcut to another file.
You can create a soft link using the ln command. But what about deleting them?
There is no special command for removing symbolic links in Linux. You can use the rm command which is also used for deleting files and directories.
You may also the unlink command here. Don't go by its name. It's not only for deleting links; it can also delete files.
Let's see it in detail.
Delete symbolic links using rm command
All you have to do is to give the name of the path of the link to the command:
Let's see it with an example. Can you identify the soft link in the long list output of ls command?
[email protected]:~/test$ ls -l total 4708 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json -rw-rw-r-- 1 abhishek abhishek 311 Sep 22 12:19 line.txt lrwxrwxrwx 1 abhishek abhishek 26 Oct 17 11:24 mylink -> ./Documents/sample-mark.md -rw-rw-r-- 1 abhishek abhishek 106 Sep 27 20:39 redirects.json -rw-r--r-- 1 abhishek abhishek 12817 Sep 22 12:28 sample.txt
The link is called mylink. You can identify it in the long listing as it starts with the character
l (for link) and the name shows the file it points to.
Let me delete the link and verify it:
[email protected]:~/test$ rm mylink [email protected]:~/test$ ls -l total 4708 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json -rw-rw-r-- 1 abhishek abhishek 311 Sep 22 12:19 line.txt -rw-rw-r-- 1 abhishek abhishek 106 Sep 27 20:39 redirects.json
You can verify that the original file still exists.
Deleting multiple links
You can remove multiple symlinks at once with the rm command:
rm symlink1 symlink2 symlink3
Remove soft links using unlink command
Another way to delete soft links is by using the unlink command. It may sound like this command is only for removing links, it can delete files as well.
To remove a link with unlink, use it like this.
I'll use the same example I used earlier.
The unlink command is pretty limited. You can not delete multiple links at once with it.
Delete soft link to a directory
You can create soft links to both files and directories. While you have to remove a directory with -r option, the link to a directory doesn't need that.
Use it the same way you used to delete link to a file:
Don't use trailing slashes with the link else it will complain.
[email protected]:~/test$ rm link_to_dir/ rm: cannot remove 'link_to_dir/': Is a directory
Here's an example of deleting a soft link to directory.
rm -fbecause it will delete the contents of the directory.
Deleting hard links
Unlike soft links, a hard link is almost indistinguishable from the original file. You can only notice it with the inode number.
Can you identify the hard link and the file in the output? Pay attention to the inode numbers.
[email protected]:~/test$ ls -li total 4716 544057 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json 544884 -rw-rw-r-- 2 abhishek abhishek 311 Sep 22 12:19 hard_link 544884 -rw-rw-r-- 2 abhishek abhishek 311 Sep 22 12:19 line.txt 1181365 drwxrwxr-x 2 abhishek abhishek 4096 Oct 17 12:33 my_dir 546419 -rw-rw-r-- 1 abhishek abhishek 106 Sep 27 20:39 redirects.json
And deleting a hard link is the same as deleting a file.
What about deleting the linked file?
I can't think of a scenario where you would want to delete the original file while removing the soft link automatically.
Well, you can follow a symbolic link to get to the original file and use it to remove the file.
rm "$(readlink '/path/to/link')" /path/to/link
While the unlink command exists, I recommend using the rm command for deleting symbolic links. You are already familiar with it and use this command for removing files. Use it for links as well. It's one less command to remember.
Join the conversation