4

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?

  • 2
    I would look at what they are first; you might discover some of them aid in recovery of problems.... Regardless they reflect a problem somewhere given they weren't removed earlier (even if it's just user procedures, ie. a user forgetting to clean up after themselves)... – guiverc Aug 23 '19 at 10:41
  • 4
  • 4
    Reviewers: This is not primarily opinion-based. It's simply objectively not safe to do this, as the answers there explain. Notice that those answers are based on facts, references, and specific expertise. – Eliah Kagan Aug 24 '19 at 2:49
4

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:

  1. 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.

  2. 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.

  3. 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy