delete symbolic links

How to Remove (Delete) Symbolic Links in Linux

Symbolic links (known as symlinks) are special types of files that point to another file or directory. They are like shortcuts that allow you to refer to a file easily from another location. A symlink can point to another file or directory on same or different file partition. In this article, we will look at how to remove symbolic links in Linux using rm, unlink and find commands. You can use these steps on almost every Linux distribution since these commands are available on all systems.

How to Remove (Delete) Symbolic Links in Linux

In order to be able to remove a symlink, you need to have write permission for the directory that contains symlink. Else you will get “Operation Not Permitted” error. Also, remember that when you remove symlink the file that it points to does not get deleted.

So before you delete a symbolic link, it is better to use ls -l command to review your permissions as well as check if it is a symbolic link. Here is an example

$ sudo ls -l /home/data.txt
lrwxrwxrwx 1 root root 9 Apr 16  2021 /home/data.txt -> data_link

In the above output the first character ‘l’ indicates that it is a symlink. Also, the -> at the end indicates the file that it points to.

1. Remove Symbolic Links with rm

You can easily remove symlink with rm command. Here is the syntax.

$ sudo rm symlink_name

You can also delete multiple symlinks with a single command

$ sudo rm symlink1 symlink2

rm command will silently delete the symlink without any output in case of success. If there is any error, then it will display error message.

If you want to confirm before deletion, you can use -i option. Enter y to confirm, n to cancel.

$ sudo rm -i symlink_name
rm: remove symbolic link 'symlink_name'? 

Never append a trailing slash ‘/’ while deleting a symlink, whether it points to a file or directory.

2. Remove symlinks with unlink

You may also use unlink command to delete symlink but it accepts only 1 argument at a time.

$ sudo unlink symlink_name

Like rm it displays no output in case of successful deletion. Also you should not include trailing slash ‘/’ at the end of symbolic link.

3. Find & Delete Broken Links

When you move or delete the target file or directory or a symlink it does not update itself, and ends up becoming a dangling symlink, also known as broken symlink.

You can easily find and delete such broken symlinks with the following command. Replace /path/to/directory with the directory in which you want to find broken links. This will also list broken links in the subdirectories present in given location.

$ sudo find /path/to/directory -xtype l

If you only want to list broken links in the directory and not in any of its subdirectory, you can limit the search to a maximum depth of 1.

$ find /path/to/directory -maxdepth 1 -xtype l

You can easily delete the above links by adding delete keyword in your find command, as shown below. It will find and delete broken symlinks in the specified folder.

$ sudo find /path/to/directory -xtype l -delete
$ find /path/to/directory -maxdepth 1 -xtype l -delete

In this article, we have looked at how to delete or remove symbolic links in Linux.

Also read:

How to Install Zoom in Ubuntu
Shell Script to Backup Files in Directory
How to Disable Configuration File in Apache
How to Read Command Line Arguments in NodeJS
How to Generate & Verify MD5 Hash of File in Linux

Leave a Reply

Your email address will not be published.