Find and Delete Broken Symlinks on Linux and macOS

Symbolic links or symlinks are an essential file system feature of UNIX-based operating systems. These files are used for referencing another files or even directories.

Moving referenced files can cause symlinks to become broken. In case a symlink references a file by a relative path, moving the symlink can also break the link.

With the following command, broken symlinks can be found in a specified directory:

find -L [directory] -type l

This command deletes found broken symlinks immediately:

find -L [directory] -type l -delete