How to Find and Remove Broken Symlinks on Linux 2

How to Find and Remove Broken Symlinks on Linux

With Symlinks, you can have multiple access points to a file without duplicating it. Any changes made to the original file will be available in the symbolic link right away. However, if you move or delete the original file, the Symlinks to that file will be unusable or broken because it will no longer have something to point. The broken Symlink stays on the system and does not show any error until you try to use it. Therefore, it is better to remove the broken Symlinks that point to non-existent files.

This post covers how to find and remove broken Symlinks on Linux OS. We have demonstrated the commands shown here on Ubuntu 20.04, however, you can use them on any Linux distribution.

Find and Remove Broken Symlinks on Linux

In this method, we will see how to find and remove the broken Symlinks in a Linux OS.

To view the Symlinks in the current directory, run the command below:

$ ls -l

In the below output, you can see two Symlinks; one pointing to a file and the other pointing to a directory.

How to Find and Remove Broken Symlinks on Linux 3

A Symlink becomes broken or unusable when the file or directory it points to is removed or moved to another location. As we have 2 Symlinks in the current directory. Let’s delete both the file and the directory to which these Symlinks points to.

$ rm myfile
$ rm -r myonedrive

Now if we run the ls command, we will see the Symlinks are listed in red color indicating they are broken Symlinks.

How to Find and Remove Broken Symlinks on Linux 4

There is another method to list the broken Symlinks in a current directory using the Find command. This command instead of listing all the directory content, lists only the broken Symlinks.

To find all broken symbolic links in a specific directory, use the command below:

$ find /path/to/directory -xtype l

To find the broken Symlinks in the current Terminal directory, use the command below:

$ find . -xtype l

This command will list only the broken Symlinks in your current directory which you can remove one by one.

How to Find and Remove Broken Symlinks on Linux 5

You can also find and remove the broken Symlinks using the single command as follows:

$ find . -xtype l -exec rm {} ;

This command finds and removes all the broken links from the current directory. To verify if the broken links have been removed, run the command below:

$ find . -xtype l

You will see now there is no broken Symlink as they have been removed.

How to Find and Remove Broken Symlinks on Linux 6

Alternate Methods to Remove Broken Symlinks

There are two more ways that can be used to remove broken Symlinks in a Linux OS which are as follows:

Using the unlink Command

To remove a broken Symlink, type unlink followed by the Symlink name as follows:

$ unlink symlink_name

Using the rm Command

To remove a broken Symlink, type rm followed by the Symlink name as follows:

$ rm symlink_name

You can also remove multiple Symlinks at once using the rm command:

$ rm symlink1 symlink2 symlink3

Broken Symlinks should be removed from the system as they point to non-existent files or directories. In this post, we covered how to find and remove the broken Symlinks in the Linux OS using three different ways.

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Leave a Comment

Your email address will not be published. Required fields are marked *