Link to home
Start Free TrialLog in
Avatar of wesly_chen
wesly_chenFlag for United States of America

asked on

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.
Avatar of gripe
gripe

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" }}}, ".";'
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of gripe
gripe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

$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 );
}