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 install MySQL on Ubuntu 20.04Photo of How to install MySQL on Ubuntu 20.04
    in this article, tipsmake will show how to install mysql version 8.0 on ubuntu 20.04 server. by completing it, you'll have an active relational database that can be used to build your next website or app.
  • How to run Android on Linux using a virtual machinePhoto of How to run Android on Linux using a virtual machine
    in this article, tipsmake will guide you how to run android on linux using a virtual machine. let's find out right now!
  • How to mount a Linux file system using WSL2 on Windows 10Photo of How to mount a Linux file system using WSL2 on Windows 10
    starting with build 20211, windows subsystem for linux 2 (wsl2) includes a new feature that allows you to attach and mount physical drives to access the linux file system (such as ext4) which is not supported. support on windows 10.
  • How to fix Checksum with the fsck command in LinuxPhoto of How to fix Checksum with the fsck command in Linux
    the file system is responsible for the way users' data is stored and organized. if the file system is corrupt and certain parts do not work then they will lead to inconsistencies in the file system.
  • How to install and use Microsoft Defender in LinuxPhoto of How to install and use Microsoft Defender in Linux
    while many linux users outside the enterprise may not fully understand the importance of microsoft tools on linux, those inside will certainly understand that they can be invaluable.
  • 8 commands for efficient management of Linux processesPhoto of 8 commands for efficient management of Linux processes
    there are three main states in the life cycle of an application process: start, run, and stop. if you want to be a competent linux administrator, you need to know how to manage each state carefully. the following 8 commands can be used to help you manage the entire process lifecycle.