How to find and remove broken symlinks on linux

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
Sponsored
and Remove Broken Symlinks on Linux

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

See also  Canonical’s commitment to quality management

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 1

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 2

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:

See also  Secure containerised Ceph with Ubuntu container images
$ find . -xtype l

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

Sponsored

How to find and remove broken symlinks on linux 3

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 4

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:

See also  OpenStack vs AWS: which one is better for you?
$ 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.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply