Friday·10·June·2011
How to find broken symlinks //at 20:31 //by abe
Looking through the man page of find
there is no obvious
way to find broken symbolic links. But there is a simple way involving
only find:
$ find -L . -type l $ find -L . -type l -ls
The option -L
(before the path!) causes find
to follow symbolic links and the expression -type l
causes find to report only symbolic links. But as it follows symlinks,
it only reports those it can’t follow, i.e. broken ones.
The second line also shows where the broken links point to.
To easily show that this really works, just use the color indicator of
GNU ls instead of find
’s builtin -ls
:
$ find -L . -type l -exec ls -lF --color=yes '{}' +
Et voilà, all displayed links show up in red which means they’re broken.
Kudos to CodeSnippets for showing me the right
idea. And thanks to ft of zsh and grml fame for the hint about
find -exec command {} +
instead of find -exec
command {} ;
.
Hint from mika of grml fame: With zsh it is even less code to type:
% ls **/*(-@) % ls -lF **/*(-@)
Thanks, mika!
Tagged as: CLI, coreutils, find, GNU, HTH, ls, POSIX, shell
// show without comments // write a comment