check the dead symbolic link

I would like to search the "DEAD" symbolic link on my Unix/Linux system.
Is any perl command line option or simple perl script to do it?
It will be in
find / -type l -print | <perl .... >   
<perl ....> is the part to check the dead symbolic link.
Thanks.
LVL 38
wesly_chenAsked:
Who is Participating?
 
gripeCommented:
You could exclude /proc by adding the following:

perl -MFile::Find -wle'find sub { $File::Find::name !~ /^\/proc/ && -l && do { unless ( stat ) { print "$File::Find::name: dead" }}}, "/";'
0
 
gripeCommented:
You can do this entirely in perl with this one-liner:

perl -MFile::Find -wle'find sub { -l && do { unless ( stat ) { print "$File::Find::name: dead" }}}, ".";'
0
 
gripeCommented:
sorry, that should read:

perl -MFile::Find -wle'find sub { -l && do { unless ( stat ) { print "$File::Find::name: dead" }}}, "/";'

You can put any directory in the "/" part and it will search recursively from there.
0
 
gripeCommented:
Word of caution, validate your list before automatically removing any dead files you find. When I ran this I found some odd matching files in /proc that I doubt it would be a good idea to remove despite them matching the condition of being a symbolic link and pointing to an invalid file.

[root@u15170981 lib]# perl -MFile::Find -wle'find sub { -l && do { unless ( stat ) { print "$File::Find::name: dead" }}}, "/";'
/lib/libnss_wins.so: dead
/lib/libnss_winbind.so: dead
/lib/modules/2.4.25-040218/build: dead
/lib/modules/2.4.21-lufs-030704/build: dead
/proc/100/exe: dead <--
/proc/99/exe: dead <--
/proc/98/exe: dead <--
<SNIP A BUNCH OF THESE>
/proc/2/exe: dead <--
/var/lib/pgsql/bar: dead <--
/usr/share/doc/psa-proftpd-1.2.9/README.mod_sql: dead
/usr/lib/icu/Makefile.inc: dead
[root@u15170981 lib]#

All of the other matches were valid.
0
 
ITcrowCommented:

$file = `/bin/ls -ld $link`;
print "Missing file $file, expanded from $link.\n" unless( -f $file );

or

if( -l $link ) {
  print "Missing file $link.\n" unless( -f $link );
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.