How-to find broken symlinks

Here is easy way to find all broken symlinks:


for i in `find / -type l`; do [ -e $i ] || echo $i is broken; done

Prev Post: Automatic TCP Send and Receive Socket Buffer Sizing
Next Post: Encode movies for PDA
Main Page: ZulusTips index

3 Responses to “How-to find broken symlinks”

  1. Michael Says:

    Probably you should use –exec or pipe to xargs to improve speed and not to break for in case of many many linknames.

  2. Darwin Award Winner Says:

    find ~/ -xdev -type l -print0 | xargs -0 -I ‘{}’ sh -c “[ -e ‘{}’ ] || echo ‘{}’ is broken”

  3. Darwin Award Winner Says:

    Actually, that will fail on any file the contains a single quote in its name. So if you’re not too attached to your single quotes, you can use this:

    # obliterate single-quotes from file names
    find $SOME_DIRECTORY -iname “*’*” -print0 | xargs -0 rename tr/\’//d
    # Function to find and remove bad links from files that don’t have single quotes in their names
    find $SOME_DIRECTORY -type l -print0 | xargs -0 -I ‘{}’ sh -c “[ -e ‘{}’ ] || rm -f ‘{}’”

Leave a Reply

You must be logged in to post a comment.