How to Find Broken Symlinks in Linux

Broken Symlinks Feature

Symbolic Links, or symlinks, are a way to simplify management of your Linux system. This is quite common on servers or workstations, where linking one directory to another is used to have applications send information to another place in the directory tree without changing configurations – essential for maintaining a healthy system. However, the problem with using symlinks is that there’s no guarantee that you’ll always have both directories in that link. Here we show you how to find and fix broken symlinks in Linux.

There’s an incredibly helpful application called simply symlinks in most repositories. It’s a simple command-line utility that will give helpful output and options for deleting those same broken symlinks.

To install it, use the following commands:

# For Debian/Ubuntu-based distro
sudo apt install symlinks
 
# For Fedora/CentOS
sudo dnf install symlinks

There are a couple of key options for symlinks. Those are -d, which will delete dangling links, and -r, which will recursively do whatever option you specify through subdirectories.

You can also use the built-in find tool in Linux. This is a less user-friendly example, but it’s helpful to learn the find command and how it works.

First, I’ll create a symbolic link. This involves taking an existing file and using the ln command to link it to a file that doesn’t exist yet. That would be like the following example for me.

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

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

Broken Symlinks Setup

Now, I’ll break that symlink.

rm test-file.txt
Broken Symlinks Rm

You can see that even though I’ve removed the original file, the ls -l command still reports the link. This is where the problem comes in. These files might be in different directories, which makes it much harder to check if the original file is still there.

Broken Symlinks Reading Broken Link

The way to fix broken symlinks is to just delete them. It’s impossible to bring them back, so you just have to clear them from the virtual directory tree.

To report broken symlinks with the symlinks tool, use the following command:

symlinks .

Note the “.” indicating the present working directory. Change this for whichever directory you’re trying to search. The output may look like this:

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

Indicating that “linked-file.txt” is dangling and that the symlink is broken. To delete them, use the following command:

symlinks -d .

The output will look similar to last time but will also include a line for “deleted.”

Broken Symlinks Symlink D

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

find . -xtype l
Broken Symlinks Find

Note once again that the “.” representing the present working directory. This will produce a less user-friendly output but will still be helpful.

And to delete, add the delete option.

find . -xtype l -delete
Broken Symlinks Find Delete

You won’t get any output for this one, but if you run it again without the -delete option, you won’t find anything.

That’s it. Now you can easily find broken symlinks and delete them before they cause more issues. There are more tutorials on symlinks that you should check out. You can also learn more about the difference between a symlink and a hard link and when you should use them.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

John Perkins

John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.