How to Find and Delete Broken Symlinks on Linux

A terminal window on a Linux laptop.Fatmawati Achmad Zaenuri / Shutterstock

Symbolic links in Linux are a great feature, but they can break and point to nothing. Here’s how to locate broken symbolic links, examine them, and remove them from your system if necessary.

Symbolic links 101

Symbolic links, also known as “soft links” and “symbolic links”, are a form of shortcuts that can point to files and directories. A symbolic link looks like a regular file or directory in a file manager window. It also appears as an entry in a list of files in a terminal window. The file or directory that the symbolic link points to can be anywhere in the file system tree.

For example, suppose you have a symbolic link in your home directory called “dave-link” that points to a file called “text-file.txt” located elsewhere in the file system tree. The commands you use on the symbolic link are automatically applied to the file it points to. If you try to use cat or less on the symbolic link, you will actually see the contents of the “text-file.txt” file.

A standard Linux installation contains many symbolic links. Even if you don’t create them yourself, the operating system uses them. Application installation routines often use symbolic links to point to executable files. When the software is updated, the binary file is replaced with the new version and all symbolic links continue to work as before, as long as the name of the new file is the same as the old one.

We can easily see some symbolic links using ls in the root directory. Some entries are displayed in a different color – on our Ubuntu 20.10 test machine, they are displayed in light blue.

We type the following:

ls /

ls / in a terminal window.

We can take a closer look using the -l (long list) option. We type the following command to look at all the “lib” entries and the single “bin” entry:

ls -l / lib * / bin

ls -l / lib * / bin in a terminal window.

At the start of each line is an “l”, which indicates that the item is a symbolic link. The text after “->” shows what the symbolic link points to. In our example, the targets are all directories.

The permissions are listed as read, write, and execute for owner, group, and others. These are bogus entries by default. They do not reflect the actual permissions on the objects to which the symbolic links point. The permissions on the target file or directory take precedence and are respected by the file system.

Broken symbolic links

A symbolic link is broken (or left hanging) when the file it points to is deleted or moved to another location. If an application’s uninstall routine does not work properly or is interrupted before completion, you may end up with broken symbolic links.

If someone manually deletes a file without knowing that the symbolic links point to them, those symbolic links will no longer work. They will look like road signs pointing to a town that has been bulldozed.

We can easily see this behavior by using a symbolic link called “hello” in the current directory. We type the following, using ls to see it:

ls -l

It points to a program called “htg” in a directory called “bin”. If we “run” the symbolic link, it runs the program for us:

./Hello

We can now check if this is happening by directly running the program:

../bin/htg

As expected, we get the same answer. Let’s delete the program file:

rm ../bin/htg

Now when we look at the symbolic link we see that it is listed in red because Linux knows it is broken. It also tells us what it was pointing to, so we can replace the file, recompile the program, or do whatever is necessary to fix the symbolic link.

Note that if we try to run the symbolic link, the error we get refers to the name of the symbolic link, rather than the name of the program the symbolic link points to.

We type the following:

./Hello

ls -l in a terminal window.

Finding Broken Symbolic Links

Most modern versions of find have the xtype (extended type) option, which simplifies the search for broken symbolic links. We’ll use the l flag with xtype, to tell it to look for links. Using find and xtype as follows, without any of the other type flags, forces xtype to return broken links:

find . -xtype l

find -xtype l in a terminal window.

Running the command in our home test directory finds quite a few broken symbolic links. Note that the search is recursive by default, so it automatically searches all subdirectories.

The “hello” symbolic link that we have broken on purpose is listed, as expected. One of the other symbolic links is related to the Firefox browser, and the rest are associated with snaps.

If we route the output through wc with the -l (lines) option, we can count the lines, which is the same as counting broken symbolic links.

We type the following:

find . -xtype l | wc -l

find . -xtype l | wc -l in a terminal window.

We are informed that we have 24 broken symbolic links pointing to nothing.

Find, examine, then delete

Before you rush in and remove any broken symbolic links, go through the results of the find command. Check if there is a valid reason for one of the broken symbolic links.

Sometimes the symbolic link can be the problem, rather than the target file. If the symbolic link was created incorrectly, it may point to nothing, but the real target is present. Recreating the symbolic link would be the fix in this case.

It’s also possible that a seemingly broken symbolic link could be used as something else, such as a file lock flag or other go / no go flag. Firefox does this; this is the first symbolic link on our list. However, Firefox is not used on our test machine, so we can safely remove it.

It is also possible that the target is only present periodically, and this is the expected (and desired) behavior of this particular software. Maybe the target file is copied from another machine or from the cloud, performs its function, and then gets deleted again, to be replaced with a different program in the next cycle.

The broken symbolic link can also be a symptom of a failed software installation. In this case, instead of removing the symbolic link, you must correct it manually or repeat the installation.

Once you fix the broken links that you need to keep, repeat the command to perform the search. Fixed symbolic links should then be absent from the search results.

For security reasons, it is best to limit symbolic link deletions to your own directories. Be extremely careful when running these commands as root or on system directories.

Remove broken symbolic links

The -exec (run) option executes commands against the search results. We are going to use rm to remove each broken symbolic link. The string {} is replaced with the name of each broken symbolic link when each is discovered by find.

We need to use a semicolon (;) to end the list of commands we want -exec to run. We’ll use a backslash () to “escape” the semicolon, so it’s treated as part of the find command, rather than something Bash should act on.

We type the following:

find . -xtype l -exec rm {} ;

find . -xtype l -exec rm {} ; in a terminal window.

We returned to the command prompt with no indication that anything had happened. To verify that the broken links have been removed, we repeat the command to find them, like so:

find . -xtype l

find . -xtype l in a terminal window.

There are no matching results, which means the broken symbolic links have been removed.

Remember to check first

Again, always take the time to review a list of symbolic links before running the command to remove them. You can avoid deleting the ones you are not sure about by running the command to delete them in the correct directories.

For example, above, we could have run the command in the “.snap” directory and then manually removed the solitary “hello” symbolic link. This would have left the Firefox lock symbolic link intact.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.