Broken symbolic links, how to find and remove them from Ubuntu

about broken symbolic links

In the next article we will take a look at how to find and remove broken symbolic links in Ubuntu. Symbolic links are a way to simplify the management of a Gnu / Linux system. These are used on both servers and workstations, where linking from one directory to another is used for applications to send information elsewhere in the directory tree.

A symbolic link on Unix or Gnu / Linux systems, it indicates an access to an existing directory or file that is in a different place within the directory structure. A modification made using this link will be reflected in the original, but conversely, if the link is removed, the file or directory it refers to will not be removed. Another less common option is to use a hard link (hard link). In this case, the deletion of the link causes the deletion of the file or directory, if it was the last hard link to the file.

An advantage of the symbolic link over hard links is that it is possible to make symbolic links that point to objects in file systems found on other devices, or to partitions within the same device. The command normally used to manage links (both harsh and symbolic) is ln.

Symlinks, a tool to manage symbolic links

There is an application called symlinks in most Gnu / Linux repositories. This is a simple command line utility, which will provide useful results and options to manage symbolic links. To install this tool on Ubuntu, we will only have to open a terminal (Ctrl + Alt + T) and execute the command:

install symlinks

sudo apt install symlinks

With symlinks we can use different options to work with symbolic links. Among them we can highlight the option -d, which will remove the links that are left hanging. Another interesting to know is the option -r, which will do recursively through the subdirectories any option that we specify to this tool.

help symlinks

A basic example

To start with this example, let’s first create a symbolic link. We can do this by taking an existing file and using the ln command to link it to a file that doesn’t exist yet. The commands to create this example would be the following:

touch archivo-ejemplo.txt

ln -s archivo-ejemplo.txt link-archivo-ejemplo.txt

Then we are going to use the command ls to check that the link we just created already exists in our system.

link created

The next thing we will do is break the symlink we just created.

rm archivo-ejemplo.txt

broken link

Even though we just deleted the original file, as you can see in the above screenshot, the command ls -l still reports that the link is still there. This is where the problem lies, as this link would get stuck in a file that no longer exists. The files in this example could also be in different directories, which could make it more difficult to check if the original file is still there.

Find and repair broken symbolic links

The way to fix broken symbolic links is to simply remove them. It is impossible to recover them, so we will only need to delete them from the directory tree.

For check for broken symlinks let’s use the symlinks tool. We will only have to use the following command:

symlinks .

It is important to look at the point (.) at the end of the command, as it indicates the current working directory. This can be changed with any path to indicate a directory in which we are trying to search. The previous command will show us an output like the following:

look for broken links

Indicates that ‘link-file-example.txt‘is hanging and that the symbolic link is broken. To eliminate it we will only have to use the same command as before, adding the option -d:

removing broken link

symlinks -d .

The output that the terminal will return will show something similar to the last time, but this time it will also include the line ‘deleted.

Continuing with the previous example, if we had not used the last command, to search for broken symlinks we could also use find as follows:

how to search for broken links with find

find . -xtype l

As with the symlinks tool, the period (.) represents the current working directory. For remove the broken symbolic links, we will only have to add the option –delete as it’s shown in the following:

find . -xtype l -delete

This command will not show any results, but if we execute it again without the option -delete, we will not see anything on the screen. This will be an indication that the broken symbolic links have been removed.

Uninstall

Uninstall symlinks it is as simple as installing it. We will only need to open a terminal (Ctrl + Alt + T) and execute the command:

uninstall symlinks

sudo apt remove symlinks

With these simple steps, all users can easily find broken symbolic links, and remove them before they can cause problems.

Add Comment