The Dead Shortcut Problem
A symbolic link (symlink) in Ubuntu Linux acts as a shortcut pointing to another file. However, if a junior administrator accidentally deletes the original file (the target) but forgets to delete the symlink pointing to it, the shortcut becomes “broken.” When a program like a web server or a database engine attempts to follow that broken shortcut, it crashes, throwing a “file not found” error. Manually clicking on thousands of shortcuts to see if they still work is impossible. To clean up a messy server architecture, you must use the terminal to actively scan for and isolate these dead shortcuts.
Using the find Command with -xtype
The standard Linux find command includes a specialized flag designed specifically to test the integrity of symbolic links during a search.
- Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
- Determine the directory you want to audit. For this example, we will check the
/var/www/folder. - To search for broken links, type the following command exactly:
sudo find /var/www/ -xtype l- Press Enter and provide your administrator password.
Understanding the Syntax and Cleaning Up
Normally, when you use the standard -type l flag, the find command simply asks “Is this file a shortcut?” and returns it. By changing the flag to -xtype l, you force the find command to actually follow the shortcut to its destination. If the destination doesn’t exist, the command flags it as broken and prints it to your screen.
If you run the command and the terminal outputs a massive list of broken, useless symlinks, you can instruct the system to instantly delete all of them by appending the -delete flag to the very end of your search string:
sudo find /var/www/ -xtype l -delete
This command instantly sweeps the directory, securely erasing only the broken shortcuts while leaving all functional symlinks and real data perfectly intact.