Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

check the dead symbolic link

Posted on 2004-10-28
5
Medium Priority
?
1,698 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:wesly_chen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 13

Expert Comment

by:gripe
ID: 12439467
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
 
LVL 13

Expert Comment

by:gripe
ID: 12439475
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
 
LVL 13

Expert Comment

by:gripe
ID: 12439518
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
 
LVL 13

Accepted Solution

by:
gripe earned 1500 total points
ID: 12439529
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12439533

$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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question