How to Remove (Delete) Symbolic Links in Linux
4 min read
A symbolic link, also known as a symlink, is a special type of file that points to another file or directory on your machine. It is something like a shortcut in Windows. A symlink can point to a file or a directory on the same or a different filesystem or partition. With symlinks, you can organize your files and folders more efficiently and access them more easily.
This guide will show you how to remove (delete) symbolic links in Linux/UNIX systems using the
Before You Begin
To remove a symlink, you must have writing permissions on the directory containing the symlink. Otherwise, you will get an “Operation not permitted” error.
When you remove a symlink, the file it refers to remains intact and unaffected. No changes or modifications will be made to the file itself.
command to check whether a given file is a symbolic link and find the file or directory to which the symbolic link points.
ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7
The first character, “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.
Remove Symbolic Links with
command removes given files and directories.
To delete a symlink, invoke the
rm command followed by the symbolic link name as an argument:
The command will exit with zero, and no output will be displayed upon successful execution.
rm you can delete more than one symbolic links at once. To do that pass the names of the symlinks as arguments, separated by space:
rm symlink1 symlink2
To get prompted before removing the symlink, use the
rm -i symlink_name
To confirm, type
y and press
rm: remove symbolic link 'symlink_name'?
If the symbolic link points to a directory, do not append the
/ trailing slash at the end. Otherwise, you will get an error:
rm: cannot remove 'symlink_to_dir/': Is a directory
If the argument’s name ends with
rm command assumes that the file is a directory. The error happens because, when used without the
rm cannot delete directories.
To be on the safe side, never use the
-r option when removing symbolic links with
rm. For example, if you type:
rm -f symlink_to_dir/
The contents of the target directory will be deleted.
Remove Symbolic Links with
command deletes a given file. Unlike
unlink accepts only a single argument.
To delete a symbolic link, run the
unlink command followed by the symlink name as an argument:
If the command executes successfully, it displays no output.
Do not append the
/ trailing slash at the end of the symlink name because
unlink cannot remove directories.
Find and Delete Broken Symbolic Links
If the source file is deleted or moved to a different location, the symbolic file will be left dangling (broken) and no longer work.
If you delete or move the source file to a different location, the symbolic file will be left dangling (broken).
To find all broken symbolic links under a given directory, run the following command:
find /path/to/directory -xtype l
The command will list all broken links under the directory and its subdirectories.
If you want to exclude the symlinks that are contained in the subdirectories, pass the
-maxdepth 1 option to
find /path/to/directory -maxdepth 1 -xtype l
Once you find the broken symlinks, you can either manually remove them with
unlink or use the
-delete option of the
find /path/to/directory -xtype l -delete
To remove a symbolic link, use either the
unlink command followed by the name of the symlink as an argument. Do not append a trailing slash to the symlink name when removing a symbolic link that points to a directory.
If you have any questions or feedback, feel free to leave a comment.