How to find broken symlinks in Linux

This is quite common on servers or workstations, where linking one directory to another is used for applications to send information elsewhere in the tree without changing configuration - Essential for maintaining a healthy system. However, the problem with using a symlink is that there is no guarantee that you will always have both directories under the link. 

Tools to repair broken symlinks

There is a very useful application called simply symlinks in most app stores. This is a simple command line utility that will provide useful output and options for removing those broken symlinks.

To install it, use the following commands:

# For Debian / Ubuntu-based distributions:

sudo apt install symlinks

# For Fedora / CentOS:

sudo dnf install symlinks

There are several main options for symlinks. That is -d , will remove dangling links, and -r , will perform any options you specify through subfolders.

You can also use the find tool built into Linux. This is a less user-friendly example, but will be helpful if you learn the find command and how it works.

Break down symlinks

First, we will create a symlink. This involves retrieving an existing file and using the ln command to associate that file with a file that doesn't exist. That should look like the following example.

touch test-file.txt ln -s test-file.txt linked-file.txt

You can see from the ls command that the link exists.

How to find broken symlinks in Linux Picture 1

Now, the post will break that symlink.

rm test-file.txt

You can see that even though the original file has been deleted, the ls -l command still reports the link. This is the cause of the problem. These files can be in different folders, which causes checking if the original file is still there.

How to find broken symlinks in Linux Picture 2

Find and fix broken symlinks

The way to fix broken symlinks is to simply delete them. Damaged symlinks cannot be restored, so simply delete them from the virtual directory tree.

To report broken symlinks using the symlinks tool use the following command:

symlinks .

Note the '.' indicates the current working directory. Change this for whatever folder you're trying to find. The output might look like this:

dangling: /home/jperkins/linked-file.txt -> test-file.txt

Indicates that 'linked-file.txt' is hanging and that the symlink is down. To remove them, use the following command:

symlinks -d .

The output will look the same as last time but include a line for "deleted".

How to find broken symlinks in Linux Picture 3

To fix broken symlinks with find , use the following command:

find . -xtype l

How to find broken symlinks in Linux Picture 4

Note again that the '.' represents the current working directory. This will produce a less user-friendly but still useful output.

And to remove, add a delete option .

find . -xtype l -delete

How to find broken symlinks in Linux Picture 5

You won't get any output for this command, but if you run it again without the -delete option , you won't see the symlink broken error anymore.

5 ★ | 1 Vote

May be interested

  • How to find out if the package is installed in LinuxHow to find out if the package is installed in Linux
    linux packages are just a collection of software and the way they are installed varies depending on the distribution. there are several ways to quickly check if a package is currently installed.
  • Why You Won't Regret Leaving Windows for LinuxWhy You Won't Regret Leaving Windows for Linux
    if you have the courage to leave, you will find that linux is a pretty good alternative. after leaving windows for linux, many people have found it to be a wise choice.
  • Check and detect broken links on any websiteCheck and detect broken links on any website
    while browsing the web to search for documents or software, you will often see links that are no longer usable because the article has been created for a long time, so some links in it no longer exist. in.
  • Top 10 best tips to protect your smartphone the bestTop 10 best tips to protect your smartphone the best
    if your beloved smartphone falls apart, it will definitely ruin your day. as the prices of smartphones get higher and higher, we get more and more worried about damaging such a valuable device.
  • How to use Isof command on LinuxHow to use Isof command on Linux
    if everything in linux is a file, you'll have more than just the file on your hard drive. this article will show you how to use the lsof command to see all other devices and processes processed as a file.
  • How to Find Files in LinuxHow to Find Files in Linux
    if you don't know how, finding files in linux can be quite difficult. here, the best way is to use a few different terminal commands. they are much more powerful than the simple search features on other operating systems, and when you know how to use them well, you will have complete control over your files.
  • How to use the which command in LinuxHow to use the which command in Linux
    the which command in linux determines the executable binary, which will execute when you issue a command to the shell. if you have different versions of the same program on your computer, you can use which to find out which shell will use.
  • 6 best Linux distributions for Windows users6 best Linux distributions for Windows users
    linux is the most known and used open source operating system. whether you are looking for a suitable operating system for your laptop, workstation, desktop, gaming, a/v editing or server, you will always find the right linux distribution for your needs. your specific needs.
  • Some tips to see if your smart TV is really brokenSome tips to see if your smart TV is really broken
    when a smart tv starts malfunctioning, many people's first thought is that it's ready to become e-waste. but instead of burning through cash, there are a few tips to see if your smart tv is actually broken.
  • Search for files and directories in Linux using the command line interfaceSearch for files and directories in Linux using the command line interface
    you can use the graphical file management utility to find files in linux like nautilus in gnome, dolphin in kde and thunar in xfce. however, there are several ways to use the command line to find files in any linux desktop management utility.