check the dead symbolic link

Posted on 2004-10-28
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.
Question by:wesly_chen
    LVL 13

    Expert Comment

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

    Expert Comment

    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.
    LVL 13

    Expert Comment

    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/ dead
    /lib/ 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 <--
    /proc/2/exe: dead <--
    /var/lib/pgsql/bar: dead <--
    /usr/share/doc/psa-proftpd-1.2.9/README.mod_sql: dead
    /usr/lib/icu/ dead
    [root@u15170981 lib]#

    All of the other matches were valid.
    LVL 13

    Accepted Solution

    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" }}}, "/";'
    LVL 5

    Expert Comment


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


    if( -l $link ) {
      print "Missing file $link.\n" unless( -f $link );

    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 (…
    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