Home Linux Tips & Tricks How To Find And Delete Broken Symlinks In Linux

How To Find And Delete Broken Symlinks In Linux

Remove broken soft links or symlinks

By sk
Published: Updated: 6.6K views

A while ago, we explained what is Soft link and Hard link with practical examples. At times you might forget to delete the unused soft links or symlinks that don't point anywhere. Keeping unused links on the filesystem doesn't harm the system, except occupying a little bit of disk space. So there is no point of keeping them. Just find and delete the broken symlinks on your Linux system.

There could be many ways to find broken symlinks or soft links in Linux. Here I have given two methods.

Symlinks is a command line, symbolic link maintenance utility. It scans the given directory and lists the symbolic links if there are any.

Symlinks is available on the official repositories of many Linux distributions.

To install Symlinks on CentOS, run:

$ sudo yum install symlinks

On Fedora:

$ sudo dnf install symlinks

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install symlinks

On Ubuntu , you need to enable the [universe] repository.

$ sudo add-apt-repository universe

After installing Symlinks, run the following command to find and delete broken symbolic links:

$ symlinks -dr <directory-path>

The above command will scan for broken symbolic links in the given directory and delete them.

Here,

  • d - delete dangling symbolic links
  • r - recursive

Let me show you an example. I am going to create a file named "abc" in the current directory.

$ touch abc

Next I create a symlink called 'xyz' for abc file:

$ ln -s abc xyz

Now delete the actual file i.e. abc.

$ rm abc

As you may know already, once we deleted the actual file, the symlink has no value. Because it is just shortcut and is broken now.

To find the broken symlinks, run:

$ symlinks .

Please note the dot (.) at the end. It means that we are searching for broken links in the current directory.

Sample output:

dangling: /home/sk/xyz -> abc

Here, xyz is dangling because we deleted the actual source file "abc". So let us delete this broken link using command:

$ symlinks -dr .

Sample output:

dangling: /home/sk/xyz -> abc
deleted: /home/sk/xyz -> abc
Find and delete broken symlinks using symlinks utility
Find and delete broken symlinks using symlinks utility

Finally, run symlinks command once again to make sure if there are any leftover broken links.

We can also use find command to delete broken symlinks.

To find broken symlinks, but not delete them, run:

$ find . -xtype l

This command will lists the broken symlinks in the current directory. If you want to search for broken symlinks on a specific path, for example /usr/local/, do:

$ find /usr/local/ -xtype l

Output:

Find broken symlinks using find command
Find broken symlinks using find command

The above command will search for broken links in /usr/local/ and its sub-directories. As you can see in the above output, there are many broken symlinks.

Alternatively, use the following command to list the unused symlinks:

$ find /usr/local/ -xtype l ! -exec test -e {} \; -print

It is also possible to check where the broken links point. It is a good practice to make sure that we are deleting the intended symlinks.

$ find /usr/local/ -xtype l -exec ls -l {} \+

Sample output:

lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/drracket -> /home/sk/racket/bin/drracket
lrwxrwxrwx 1 root root 27 Jan 21 08:53 /usr/local/bin/gracket -> /home/sk/racket/bin/gracket
lrwxrwxrwx 1 root root 32 Jan 21 08:53 /usr/local/bin/gracket-text -> /home/sk/racket/bin/gracket-text
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/local/bin/mred -> /home/sk/racket/bin/mred
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/local/bin/mred-text -> /home/sk/racket/bin/mred-text
lrwxrwxrwx 1 root root 23 Jan 21 08:53 /usr/local/bin/mzc -> /home/sk/racket/bin/mzc
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/local/bin/mzpp -> /home/sk/racket/bin/mzpp
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/mzscheme -> /home/sk/racket/bin/mzscheme
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/local/bin/mztext -> /home/sk/racket/bin/mztext
lrwxrwxrwx 1 root root 30 Jan 21 08:53 /usr/local/bin/pdf-slatex -> /home/sk/racket/bin/pdf-slatex
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/local/bin/plt-games -> /home/sk/racket/bin/plt-games
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/plt-help -> /home/sk/racket/bin/plt-help
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/plt-r5rs -> /home/sk/racket/bin/plt-r5rs
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/plt-r6rs -> /home/sk/racket/bin/plt-r6rs
lrwxrwxrwx 1 root root 34 Jan 21 08:53 /usr/local/bin/plt-web-server -> /home/sk/racket/bin/plt-web-server
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/local/bin/racket -> /home/sk/racket/bin/racket
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/local/bin/raco -> /home/sk/racket/bin/raco
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/local/bin/scribble -> /home/sk/racket/bin/scribble
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/local/bin/setup-plt -> /home/sk/racket/bin/setup-plt
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/local/bin/slatex -> /home/sk/racket/bin/slatex
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/local/bin/slideshow -> /home/sk/racket/bin/slideshow
lrwxrwxrwx 1 root root 27 Jan 21 08:53 /usr/local/bin/swindle -> /home/sk/racket/bin/swindle

To delete all of the broken links at once, run:

$ sudo find /usr/local/ -xtype l -delete

Or,

$ sudo find /usr/local/ -xtype l ! -exec test -e {} \; -delete

Replace /usr/local/ with your own path.

Hope it helps.

You May Also Like

3 comments

olivia January 24, 2020 - 4:19 pm

thanks! this is just exactly what i had been looking for 😀

Reply
jsn January 24, 2020 - 6:19 pm

OMG, why this ‘$ touch abc.txt’? Touch is not for creating files, it is slow. Use only a redirection, like so ‘$ > abc.txt’

Reply
Gemini Jim June 15, 2020 - 7:35 pm

So is ‘symlinks’ just a wrapper script for ‘find’?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More