Monday, December 6, 2010

Howto - Find broken symbolic links

2 cool ways to find broken symbolic links on your system:
Bash:
for i in `find /`; do if (test -h $i); then file $i|grep broken; fi; done

Output:
./bar_link: broken symbolic link to `/etc/foo'



Any Shell:
find / -type l ! -exec test -r {} \; -print

Output:
./bar_link

When measuring the results with "time" command - the 2nd command wins, so if performance is critical in your case, use the 2nd one :)

2 comments:

Sasha said...

Try this one =)

#find . -type l -and -not -exec test -e {} \; -print

Sasha.

Paul Podolny said...

Good one, 10x.
Now need to test with "time" which one wins with performance...