In Linux, creating symbolic links is quite easy but what if you want to remove them?
There is an unlink command. But it is not dedicated to links. It is used for removing files as well as link.
In fact, you can use the good old reliable rm command to delete the links.
Let me show how you can use the rm command to remove symbolic links.
How to remove symbolic links in Ubuntu Linux
The rm command is mainly used to remove files from Linux but it can also be used to remove symbolic links.
How? Just follow the given simple syntax:
Don't know how to find the symbolic link? You can use the ls or tree command:
As you can see, my symlink is
Linuxmint which is pointing to
So if I were to remove a
Linuxmint symbolic link, then, I'd have to use the following command:
How to remove multiple symbolic links
To remove multiple symlinks, all you have to do is append multiple symbolic filenames:
rm symlink1 symlink2 symlink3
Here, I have two symbolic links:
So if I want to remove both at once, I'd have to use the following command:
rm Linuxmint Pop-OS
How to remove symbolic link pointing to a Directory
You can create a symlink to a directory so what about removing it? Well, the syntax is the same as you did with files:
Remember, never use
/ while removing the symbolic link to the directory or it will throw the following error:
So let's say I want to remove the
ISO symlink which is pointing to another directory. Then I will be using the following:
Bonus: Remove hardlink from Ubuntu
You may have created a hard link and want to remove it.
First, it is almost impossible to distinguish between a normal file and a hard link. To do so, you'd have to pay attention to the inode number as it points to the data block so it will be the same as the original file.
Here, I have created a hard link which is pointing to the file in the same directory so I can show the inode numbers easily:
Once you find the symbolic link, you can use the rm command to remove it:
And it should remove it!
Hardlinks are much more interesting than you think!
Softlinks are cool but did you know that hard link directly points to the storage block so even if you remove the file, you can still use the hard link to access the data?
There's more to it. And if you're interested, you can refer to our detailed guide:
I hope you will find this guide helpful. And if you have any queries, feel free to ask in the comments.