How to Find Broken Symlinks in Linux

This quick tip teaches you various ways to find all the broken symlinks in Linux command line. You'll also learn to manage those broken soft links.

Abhishek Prakash
Abhishek Prakash

Table of Contents

In an earlier article, I explained what is symbolic link in Linux. Symbolic links or symlinks or soft links are used for creating shortcuts to other files in Linux. Symbolic links are heavily used in linking libraries in Linux.

When the original file to which the link pointing is deleted, the link becomes dangling link. Keeping such links may not be a good idea.

Now the question arises, how do you find the broken symbolic links in Linux? Some distributions show broken links in different color but that’s not a way to find all such broken links.

Find Broken Symbolic Links in Linux

In this tutorial, I’ll show you a couple of ways for finding dangling links in Linux.

The amazing find command can be used for finding broken soft links as well. The newer versions of this command provide a dedicated option for this task.

To find all the dangling links in the current directory and its subdirectories, you can use the find command like this:

find . -xtype l

It’s output will list all the broken links:

find . -xtype l

You can also use a dedicated command named symlinks to for handling dangling links.

You may have to install the symlinks command. It is available in Debian. If you are using Ubuntu, you’ll have to enable Universe repository.

sudo apt install symlinks

Once installed, you can use it in the following manner:

symlinks [options] path_to_directory

So, if you have to find the broken links in current directory, you can use it like this:

symlinks .
dangling: /home/abhishek/tutorials/target_link2 -> newdir/test_dir/myzip

Did you notice something? It lists only one broken link while the find command had two broken links. It’s because if you want to search recursively, you’ll have to specify the option:

symlinks -r directory

That’s not it. You can also choose to delete all the broken links with the option -d of the symlinks command.

symlinks -d .
dangling: /home/abhishek/tutorials/target_link2 -> newdir/test_dir/myzip
deleted:  /home/abhishek/tutorials/target_link2 -> newdir/test_dir/myzip

In fact, symlinks can be used to properly manage soft links in your system. It can warn you about links across file systems, change messy links to relative or shorten lengthy links.

In Linux, there could be several ways of doing a task. Finding broken symlinks is the same. I showed you two ways but there can certainly other ways. If you have some other favorite command for managing dangling links, do share it with us in the comment section.

On a related topic, you may want to read about the concept of hard links in Linux.

Join the conversation.