How to Automatically Check for Broken Symbolic Links in Linux Using the find Command

Symbolic links (or symlinks) in Linux are incredibly useful tools that act as shortcuts, pointing from one directory to a file or folder located elsewhere on the filesystem. However, if the original target file is moved, renamed, or deleted, the symbolic link remains behind. This creates a “broken” or “orphaned” link that points to a non-existent path. Over time, these broken links can clutter your system and cause automated backup scripts or application deployments to fail.

Instead of manually checking directories, you can use the powerful find command to scan your entire system and identify every broken symbolic link automatically.

Locating Broken Symbolic Links

The find command includes a specific test flag designed to evaluate the state of symbolic links. The -xtype l flag instructs find to return only symbolic links where the target file does not exist.

To search a specific directory (for example, your home directory), open your terminal and run:

find ~/ -xtype l

If you want to perform a comprehensive scan of the entire root filesystem, you will need to execute the command with sudo privileges to avoid permission denied errors in system directories. You can also append 2>/dev/null to suppress any lingering error messages regarding inaccessible virtual filesystems (like /proc):

sudo find / -xtype l 2>/dev/null

The terminal will output a list of absolute paths pointing to every broken symlink it discovers.

Automatically Deleting Broken Links

Once you have verified the list of broken links and are confident they are safe to remove, you can instruct the find command to delete them immediately upon discovery by appending the -delete action.

Warning: Always run the standard search command first to review the output before appending the delete flag, as this action cannot be undone.

find ~/ -xtype l -delete

This command operates silently. It will scan the directory, identify the broken links, and permanently delete the orphaned shortcut files without prompting for confirmation. It will not touch the (already missing) original files, nor will it delete healthy, functioning symbolic links.

Alternative: Using the -exec Flag

If you are using an older version of the find utility that does not support the -delete flag, you can achieve the exact same result by piping the discovered files to the rm command using the -exec argument:

find ~/ -xtype l -exec rm {} +

This syntax bundles the broken link paths and executes the removal command efficiently, keeping your Linux filesystem clean and organized.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.