How do I find and report on broken symbolic links automatically?
The problem with -L
is that is has the side effect of expanding the search into sub-directories that are the targets of symlinks
, which might not be expected or desired.
With GNU findutils
version of find
:
<!-- language: bash -->
find /path/to/search -xtype l
except that doesn't find cyclic symbolic links.
-execdir
in the other answer isn't as portable, so distilling it down to a portable solution that finds broken symbolic links, including cyclic links:
<!-- language: bash -->
find /path/to/search -type l -exec test ! -e {} \; -print
See this question, or ynform.org for further discussion. Also see the findutils documentation for details. The ynform.org link also presents a method for detecting only cyclic links.
Many ways to skin a cat
This is quite portable (-L is posix requirement)
find -L /path/you/care/about -type l 2>/dev/null | mail -s "Broken symlinks detected" [email protected]
You didn't define broken, the above would send you broken links that have no target in the part of the filesystem you care about. It also reports on stderr filesystem loops and Too many levels of symbolic link etc. problems. If you care about them too then redirect stderr to your mail
find -L /path/you/care/about -type l 2>&1 | mail ...
If your find supports it -readable
is useful and fast
find /path/you/care/about -type l ! -readable | mail ...
The above includes links with Too many levels of symbolic link problems in it's output but not filesystem loops.
If the parts of the filesystem you care about have different paths then
find /path/you/care/about /another/path ...