Is there a way to find all symbolic links that don’t point anywere?

Bash Code
find ./ -type l
[ad type=”banner”]

will give me all symbolic links, but makes no distinction between links that go somewhere and links that don’t.

You are currently doing:

Python Code
find ./ -type l -exec file {} \; |grep broken

The symlinks command can be used to identify symlinks with a variety of characteristics. For instance:

Bash Code
$ rm a
$ ln -s a b
$ symlinks .
dangling: /tmp/b -> a

find -L can have unexpected consequence of expanding the search into symlinked directories, so isn’t the optimal approach.

Python Code
find /path/to/search -xtype l

[ad type=”banner”]

is the more concise, and logically identical command to

Python
find /path/to/search -type l -xtype l

None of the solutions presented so far will detect cyclic symlinks, which is another type of breakage. this question addresses portability.
To recap, the portable way to find broken symbolic links, including cyclic links, is:

Python Code
find /path/to/search -type l -exec test ! -e {} \; -print

To display a list of the broken links, replacing ${directory} with the desired root directory Edit

Python Code
find /path/to/search -xtype l
for f in $(find /path/to/search -type l); do if [ ! -e "$f" ]; then echo "$f"; fi; done

To display a list of the broken links,

Python Code
find -xtype l -exec ls -l {} \;
find /usr/local/bin -type l | while read f; do if [ ! -e "$f" ]; then ls -l "$f"; fi; done

all one line, if there are broken links it will display them with ls -l

This will print out the names of broken symlinks in the current directory.

Python Code
for l in $(find . -type l); do cd $(dirname $l); if [ ! -e "$(readlink $(basename $l))" ];
then echo $l; fi; cd - > /dev/null; done

Works in Bash.

[ad type=”banner”]

Categorized in: