Solved

check the dead symbolic link

Posted on 2004-10-28
1,696 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
Question by:wesly_chen
    5 Comments
     
    LVL 13

    Expert Comment

    by: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" }}}, ".";'
    0
     
    LVL 13

    Expert Comment

    by:gripe
    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
    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:
    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

    $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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Suggested Solutions

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
    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…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now