Want to know more about deleting broken symlinks on your system? Maybe someone told you how dead symbolic links take up space on your storage device and now you want to get rid of them for good.

Luckily, there are several utilities available that you can download on your Linux computer in order to manage symbolic links. These tools will also help you in finding broken soft links and fixing them permanently.

Here we will discuss how you can report and fix broken symbolic links on your system using symlinks and find command.

Generally, every computer system has two types of links---soft links and hard links. Hard links are directory entries that link a specific name with a file present on your system. These are the original files that are stored in a particular address on your storage.

On the other hand, soft links are text strings that link two directories or files with each other. Program shortcuts are a great example of symbolic links. Suppose, file A has a symbolic link with file B. This means that file A will store the absolute or relative path to file B.

Related: How to Create a Symlink in Linux

To create a symlink:

  1. Launch the terminal by pressing Ctrl + Alt + T on your keyboard.
  2. Create a new text file named text.txt.
            touch text.txt
        
  3. Link another text file (another.txt) with the file you've just created (text.txt).
            ln -s text.txt another.txt
        

You just linked two different text files together using a symbolic link. The ln command is the default way of creating symbolic links on a Linux-based operating system. The -s in the above-mentioned command stands for symbolic links.

Related: How to Use the ls Command in Linux

The major problem with symbolic links is, if you delete or move the target file, the symbolic link is not removed automatically. Instead, the link now points to a file that is not even present on your system. Such links are known as dangling, broken, orphaned, or dead links.

First, you need to confirm that a symbolic link exists in the system. You can easily do that using the ll and grep command.

        ll | grep txt
    

The above command will list down all the symlinks associated with text files in your current working directory.

Now, if we were to break the symlink we've created above by deleting the parent text file (text.txt):

        rm text.txt
    

The symlink will break and is not removed from your system. You can confirm this by typing the ll | grep txt command in your terminal again.

Although a couple of broken symlinks won't do any harm to your computer, this can gradually become a thousand in no time. That's why there is a need to monitor dangling links on your system.

The only way to fix these broken symlinks is by deleting them. Your system contains hundreds of dangling links and no one has the time to check for these links manually. In such cases, Linux tools and commands prove to be really helpful.

Symlinks is a powerful utility that provides you with all the tools you need to manage and fix broken symlinks on your system. Since it is not installed on most of the Linux distributions by default, you will have to install it manually.

You can use Pacman to install the package on Arch Linux:

        sudo pacman -S symlinks
    

On Fedora:

        sudo dnf install symlinks
    

On CentOS:

        sudo yum install symlinks
    

On Debian based distributions:

        sudo apt-get install symlinks
    

If you are running Ubuntu, before installing the symlinks package, you will have to add the universe repository to your system's repository list.

        sudo add-apt-repository universe
    

After you have successfully installed symlinks, you need to check for orphaned links on your system. To do so, enter:

        symlinks .
    

The . (dot) character refers to the current working directory. If you want to report broken symlinks in your /home directory, you can do it by typing in:

        symlinks /home
    

If a broken link is present on your system, you will get an output that looks something like this.

        dangling: /home/sharmadeepesh/another.txt -> test.txt
    

To quickly delete the reported symlink, you can use the -d flag with the default command.

        symlinks -d .
    

To delete dangling symlinks in the /home directory, enter:

        symlinks -d /home
    

This time, the output will not only list down the broken link but will also report that the link is now deleted.

        dangling: /home/sharmadeepesh/another.txt -> test.txt
deleted: /home/sharmadeepesh/another.txt -> test.txt

To report and delete broken symbolic links in a given directory recursively, use the -dr flag with the default command, where d stands for delete and r stands for recursive.

        symlinks -dr .
    

Using the Find Command

The find command comes preinstalled on every Linux system. As the name suggests, you can search for folders and files using this command. The find command allows you to report and delete dead soft links on your system easily as well.

To list down broken symbolic links in your current working directory, type:

        find . -xtype l
    

To find broken links present in any other directory on your system, just replace the . (dot) character with the directory path. The following command will search for broken links in the /home directory.

        find /home -xtype l
    

You can also list down the broken symlinks that are not used by your system.

        find . -xtype l ! -exec test -e {} \; -print
    

To check where these broken links point, use:

        find . -xtype l -exec ls -l {} \+

Depending on the amount of broken symbolic links on your system, you will get an output that looks something like this.

        lrwxrwxrwx 1 root root 19 Feb 21 11:53 /home/sharmadeepesh/another.txt -> /home/sharmadeepesh/text.txt
    

Furthermore, you can delete broken symbolic links in one go with the following command.

        sudo find . -xtype l -delete

There's an alternative way to delete dead links as well:

        sudo find . -xtype l ! -exec test -e {} \; -delete

Symbolic Links are important to a Linux system as they ease out the process of path resolution and management on your computer. But if not taken care of, broken symbolic links can take up a huge chunk of your system storage and you won't even know about it. In such situations, utilities such as symlinks and find come into play.

If you are a beginner and want to improve your expertise in Linux, then learning new commands every now and then is the perfect approach to go for. Users should know which command they need in order to solve a given situation efficiently.