How To Find Broken Symbolic Links

By | 2018-06-27

Sometimes you may want to find and remove broken symbolic links from your systems. For example, OSSEC will complain if it finds broken symbolic links in directories it monitors. This guide should work on almost any Linux or UNIX like operating system. The following command will list broken symbolic links in the directory you specify.

find -L /path -type l
  • -L instructs find to try to follow symbolic links and display the file or directory it points to. When find with -L encounters a broken link, it will display the link itself.
  • -type l instructs find to only find and display symbolic links

Since find will try to follow symbolic links, but will only display symbolic links, it will only find broken links.

If you want to remove the broken symbolic links, use the following command:

find -L /path -type l -exec rm -i {} \;

Be careful if you decide to remove them. If you have any kind of chroot environment or container system, such as Solaris zones or FreeBSD jails, symbolic links that appear broken from the main system can be valid in their environment. Removing them in this situation can leave these environments in an unusable state.

There are other methods. Check out this Stack Exchange discussion on the topic for an interesting discussion on the topic.