I wonder if I tried this:
$ sudo -i # root user
# cd / # system folder
# find . -xtype l -delete # delete all broken link in system
Would it be safe to remove every broken symbolic link everywhere?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI wonder if I tried this:
$ sudo -i # root user
# cd / # system folder
# find . -xtype l -delete # delete all broken link in system
Would it be safe to remove every broken symbolic link everywhere?
You should not attempt to remove all the broken symbolic links on your system, especially not with find . -xtype l -delete
or any such silent mechanism.
Indiscriminate removal of broken symlinks has (as I choose to count them) three major bad effects:
It removes broken symlinks that are correct, thus creating a problem where there wasn't one. In spite of the term "broken," it is not necessarily wrong or a mistake to have a symlink whose target doesn't exist (or cannot be traversed to), and sometimes this is even relied on.
A broken symlink can be correct when the target is what needs to be created, when the target deliberately exists some but not all of the time, and in various other situations, which the strange-seeming but reasonable use of broken symlinks for locking (i.e., synchronization across processes) where there is no meaningful target.
It removes broken symlinks that shouldn't exist, which might sound like a good thing--but it does so without investigating or addressing what caused them to be broken in the first place.
For example, perhaps you've used some uninstallation procedure that leaves files that should be deleted. In that case, deleting just broken links wouldn't clean up anything else.
It removes broken symlinks that are wrong but where the problem should be solved through some other action than deleting them.
For example, maybe a symlink's target should be created or renamed, or maybe the symlink should be replaced or (for symlinks with relative paths) moved.
For more detailed information about all scenarios, as well as some not covered above, see Gilles's answer to Is there a downside to deleting all of the broken symbolic links in a system? (on Unix.SE).
In the use case you describe where you run that find
command as root, the likelihood that a symlink isn't really broken but that you just don't have permissions to traverse to its target is of course much lower. But every other point in Gilles's answer fully applies.
It's reasonable to use find . -xtype l
to find out about broken symbolic links. But you shouldn't attempt to delete them all.