Last Updated:

Finding and removing broken character links in Linux

In this tutorial, we'll look at how to find broken (i.e. broken) symbolic links, analyze them, and remove them from the Linux system if necessary.

Symbolic links


Symbolic links (or "soft links""symlinks") are a kind of shortcuts that can point to files and directories. In a file manager window (or in a terminal console), a symbolic link looks just like a regular file or directory. The file or directory that the symbolic link points to can be located anywhere in the file system.

Suppose, for example, that in your home directory called user there is a symbolic link pointing to a file called text-file.txt that is located somewhere else in the file system. Commands that you apply to a symbolic link are automatically applied to the file it references, i.e. if you try to use the command or together with a symbolic link, you will see the contents of the text-file.txt file.catless

Linux contains a large number of symbolic links. Application installers often use symbolic links to create links to executable files. When updating the software, the binary file is replaced with a new version, and all symbolic links continue to work as before as long as the name of the new file matches the old one.

We can easily see some symbolic links by applying the command to the root directory. On the Debian 11 test machine, they are highlighted in pale green:ls

ls /


We can look at them in more detail by adding an option (long listing):-l

ls -l /lib* /bin


At the beginning of each line is a letter (link), which indicates that this object is a symbolic link. The part after shows the object referenced. In our example, the targets are directories.l->

Symbolic links do not use file permissions (in other words, they always have the form ). In reality, the access rights to symbolic links are determined by the access rights to the file to which the symbolic link points.rwxrwxrwx

Create symbolic links

Before creating a symbolic link, I suggest writing a small program that we will link to our link.

To do this, open a terminal (for example, using the keyboard shortcut Ctrl+Alt+T) and create a new p-qc file.cpp:

touch p-qc.cpp


Then open this file with any text editor, for example, nano:

nano p-qc.cpp


Add the following code:



Press Ctrl+O to save the changes and Ctrl+X to exit the editor.

Let's compile our program using the g++ compiler:

g++ ravesli.cpp -o p-qc


Now let's move the compiled file of our program to other binaries in the /bin directory:

sudo mv p-qc /bin

All that's left is to create a symbolic link and associate it with /bin/p-qc. To do this, enter the command (link) along with the parameter (symbolic) and the target object (with the name of the link ():ln-s/bin/p-qc)hello

ln –s /bin/p-qc hello


We just linked a binary to a symbolic link.

Broken symbolic links




A symbolic link becomes a bit (or "hanging") link if the file associated with it is deleted or moved to another location. If someone manually deletes a file without knowing that it has symbolic links, those symbolic links will no longer work. They will look like road signs pointing to a city that was bulldozed.

To illustrate this behavior, I specifically created a symbolic hello link in the ~/Documents directory:

ls -l


You can see that the link points to a program called p-qc in the /bin directory. If we run a symbolic link, the program associated with it will start:



Next, let's turn to the program directly:



As expected, we got the same result. And now let's delete the program file:

sudo rm /bin/p-qc


The color of the symbolic link has changed. It's highlighted in red because Linux knows the link is broken. When you do this, the system continues to display the object that was referenced so that we can replace the file, recompile the program, or do other actions necessary to restore the symbolic link.

Note that if we try to run the symbolic link again, we will get a link error, not the object that the link points to:



Finding Broken Character Links (find Command)

Most modern versions of the command have an option that makes it easier to find broken symbolic links. Using a command with an option and a flag (link) will allow you to search for and return broken links:find-xtypefind-xtypel

find . -xtype l


It is worth noting that by default the search is recursive, so it is automatically performed for all subdirectories.

If we connect the above command to the command (lines), we can count the strings, which is similar to counting the broken character links:wc -l

find . -xtype l | wc -l


As you can see, we have a 1 bit character link.

Analysis of broken symbolic links

Before you remove any broken character links, carefully review the results of the . Consider whether there is a good reason for any of the symbolic links found to be a bit.find

Sometimes the problem can be with the symbolic link instead of the target file: if the symbolic link was created incorrectly, it may not point to anything, but the target file exists. In this case, recreating the symbolic link will be the solution to the problem.

It's also possible that an explicitly broken symbolic link is being used as something else, such as a block indicator. Or that the goal is present only intermittently, and this is the expected (and desired) behavior of a particular software. Perhaps the target file is copied from another machine or cloud, performs its function, and then deleted again, only to be replaced by another program in the next cycle.

A broken symbolic link can be a sign of a failed software installation. In this case, instead of removing the symbolic link, you should either fix it manually or repeat the installation. When you have corrected the required broken links, repeat the command to find the broken links. If done correctly, corrected symbolic links should no longer appear in search results.

For security reasons, it's best to remove symbolic links within your own directories. Be extremely careful when running these commands as root or in system directories.

Remove broken character links

The option (execute) runs some command that will be executed on the search results received from the command . We are going to use the command to remove each corrupted symbolic link. The part is replaced with the name of the bit of the symbolic link as each one is detected using the .-execfindrm{}find

We must use a semicolon () to complete the list of commands we want to run with . A backslash () is used to escape the semicolon, so that it is treated as part of the command :;-exec\find

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


As you can see, the command was executed without any indication that something had happened. To make sure that the broken links are removed, we repeat the command to look for them:

find . -xtype l


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

symlinks utility

symlinks is a powerful utility for managing and fixing broken symbolic links. Since symlinks is not included in the standard delivery of most Linux distributions, you will have to install this tool manually.

The installation command for this utility (in Debian 11) will look like this:

sudo aptitude install symlinks


Note: If you are using Ubuntu, you will need to add the universe repository to your system's repository list before installing symlinks:

sudo add-apt-repository universe

After successfully installing symlinks, you need to check for broken links on your system. To do this, enter the following:

symlinks .


If there is a dangling link on your system, you will get an output that looks like the screenshot above.

The symbol (dot) indicates the current working directory. If you want to get information about broken character links, for example, in the /home directory, you can do so by typing the following command:.

symlinks /home

If there is a broken link on your system, you will get something like the following result:

dangling: /home/diego/hello -> /bin/p-qc

To quickly remove a previously received broken symbolic link, you can use the flag (delete):-d

symlinks -d .

To remove broken character links in the /home directory, type:

symlinks -d /home

This time, the output will not only list the broken links found, but also report that the link has now been removed, for example:

dangling: /home/diego/hello -> /bin/p-qc
deleted: /home/diego/hello -> /bin/p-qc

To recursively search and remove broken symbolic links in a given directory, use the flags (d = delete, r = recursive):-dr

symlinks -dr .





Symbolic links are important for Linux because they make it easier to identify paths and manage them on your computer. But if you don't take care of it, broken symbolic links can take up a huge chunk of your system storage and you won't even know it. In such situations, utilities such as symlinks and the . Also, try to always take the time to review the list of symbolic links before running the command to remove them.find