In this article, we are going to show that how to delete or remove Symbolic Links in Linux Terminal.
A symbolic link (also known as a symlink) is a term or special type of file in the Linux that points to another file or directory. In other words, it is contains a reference to another file or directory in the form of an absolute or relative path.
In this article, we will remove or delete symbolic links using the rm
, unlink
, and find
commands.
How to Delete(Remove) Symbolic Links in Linux
Before remove a symlink, make sure that you have writing permissions on the directory that contains the symlink. If you don’t have writing permission then you will get “Operation not permitted” error.
You can use the ls
command to check the given file is a symlink and to find the file or directory that symlink point to.
For an example:
ls -l /usr/bin/python
Output
lrwxrwxrwx 1 root root 15 Feb 25 2021 /usr/bin/python -> python2.7
The first character l
, tells that the file is a symlink. The “->” arrow symbol indicates the file the symlink points to.
Remove Symbolic Links With rm Command
To remove a symlink, run the rm
command with the symbolic link name as an argument:
rm symlink
For delete more than one symbolic links in single command, run the command as below:
rm symlink1 symlink2
If you want to get a prompt confirmation message, use -i
option along with rm
command as show below:
rm -i symlink
When prompted, type y
and hit Enter
key.
Remove Symbolic Links With unlink Command
The unlink is a system call and command line utility that is used to delete a single files. It calls directly interfaces with the unlink system function, which is removed a given or specified file.
The unlink
command accept only a single argument, It means, you can pass only one file to delete. If you pass more than one files to remove then it show “unlink: extra operand” error.
To delete a symbolic link, run the unlink
command with the symlink name:
unlink symlink
How to Delete(Remove) Broken Symbolic Links
When you delete the source file or move the source file to a new location then the symbolic file will be broken or dangling.
You can use find command to find all broken symbolic links. Run the command below:
find /path/to/directory -xtype l
Output
/path/to/directory/symlink1
/path/to/directory/subdir/symlink2
The command above will display all the broken symbolic links under the given directory and its subdirectories.
Once get the broken symbolic links, use the -delete
option with the find
command as show below:
find /path/to/directory -xtype l -delete
you can also manually remove the broken symbolic links with rm
or unlink
as we discussed above.
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.
Leave a Reply