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

[pastacode lang=”bash” manual=”find%20.%2F%20-type%20l%0A” message=”Bash Code” highlight=”” provider=”manual”/] [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:

[pastacode lang=”python” manual=”find%20.%2F%20-type%20l%20-exec%20file%20%7B%7D%20%5C%3B%20%7Cgrep%20broken%0A” message=”Python Code” highlight=”” provider=”manual”/]

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

[pastacode lang=”bash” manual=”%24%20rm%20a%0A%24%20ln%20-s%20a%20b%0A%24%20symlinks%20.%0Adangling%3A%20%2Ftmp%2Fb%20-%3E%20a%0A” message=”Bash Code” highlight=”” provider=”manual”/]

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

[pastacode lang=”python” manual=”find%20%2Fpath%2Fto%2Fsearch%20-xtype%20l%0A” message=”Python Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

is the more concise, and logically identical command to

[pastacode lang=”python” manual=”find%20%2Fpath%2Fto%2Fsearch%20-type%20l%20-xtype%20l%0A” message=”Python” highlight=”” provider=”manual”/]

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:

[pastacode lang=”python” manual=”find%20%2Fpath%2Fto%2Fsearch%20-type%20l%20-exec%20test%20!%20-e%20%7B%7D%20%5C%3B%20-print%0A” message=”Python Code” highlight=”” provider=”manual”/]

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

[pastacode lang=”python” manual=”find%20%2Fpath%2Fto%2Fsearch%20-xtype%20l%0A%20%20%20%20%20%20for%20f%20in%20%24(find%20%2Fpath%2Fto%2Fsearch%20-type%20l)%3B%20do%20if%20%5B%20!%20-e%20%22%24f%22%20%5D%3B%20then%20echo%20%22%24f%22%3B%20fi%3B%20done%0A” message=”Python Code” highlight=”” provider=”manual”/]

To display a list of the broken links,

[pastacode lang=”python” manual=”find%20-xtype%20l%20-exec%20ls%20-l%20%7B%7D%20%5C%3B%0Afind%20%2Fusr%2Flocal%2Fbin%20-type%20l%20%7C%20while%20read%20f%3B%20do%20if%20%5B%20!%20-e%20%22%24f%22%20%5D%3B%20then%20ls%20-l%20%22%24f%22%3B%20fi%3B%20done%0A” message=”Python Code” highlight=”” provider=”manual”/]

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.

[pastacode lang=”python” manual=”for%20l%20in%20%24(find%20.%20-type%20l)%3B%20do%20cd%20%24(dirname%20%24l)%3B%20if%20%5B%20!%20-e%20%22%24(readlink%20%24(basename%20%24l))%22%20%5D%3B%0A%20%20%20%20%20%20%20%20%20then%20echo%20%20%20%24l%3B%20fi%3B%20cd%20-%20%3E%20%2Fdev%2Fnull%3B%20done%0A” message=”Python Code” highlight=”” provider=”manual”/]

Works in Bash.

[ad type=”banner”]

Categorized in: