Solved

Search for the dead symbolic link

Posted on 2004-10-28
1,416 Views
Last Modified: 2007-11-27
I would like to search the "DEAD" symbolic link on my Unix/Linux system.
I do
# find / -type l -print
to search the symbolic link.
Then how to I do to determine the link is dead.

I saw the red color in the output of "ls" if the link is dead. But how can I use the command or script to determine the dead link.
I found
find / - type l -print | perl -nle ' -e || print '
from the internet but it doesn't work for me.
0
Question by:wesly_chen
    10 Comments
     
    LVL 6

    Expert Comment

    by:blkline
    How about something like this?  (This is a quick knock-off so not very efficient)

    for ln in $(find . -type l -ls | sed -e 's/^.*>//'); do echo $ln; if [ -d $ln -o -f $ln ]; then echo "Good"; else echo "Dead"; fi; done
    0
     
    LVL 6

    Expert Comment

    by:blkline
    Duh... now that I look at this I realize that you'll only find out if the reference is good and the referenced file.  Let's try again:

    find . -type l -ls | \
    while read ln
    do
       referenced=$(echo $ln | sed -e 's/^.*>//')
        if [ -d $referenced -o -f $referenced ]; then
               echo "$ln is good."
        else
               echo "$ln is dead, Jim."
        fi
    done
    0
     
    LVL 6

    Accepted Solution

    by:
    Change:

    if [ -d $referenced -o -f $referenced ]; then

    to:

    if [ -a $referenced ]; then

    if you want to simply know if the file exists.  
    0
     
    LVL 38

    Expert Comment

    by:yuzh
    The easy way to do it use find with "-follow"

    eg:
    find /dir -type l -follow
          will return
                    "/dir/xxx No such file or directory"
                    or
                    "find: cannot follow symbolic link [whatever]:
          No such file or directory" for each broken link."

                    The output depands on your version of OS.
    0
     
    LVL 38

    Expert Comment

    by:yuzh
    You can also use the following script:

    #!/bin/ksh
         for link in `find . -type l `
           do
            cat $link > /dev/null 2> /dev/null;
            if [ $? -ne 0 ] ;  then
               echo $link
            fi
           done
    exit
    0
     
    LVL 38

    Author Comment

    by:wesly_chen
    I got the following answer for perl expert:
    perl -MFile::Find -wle'find sub { -l && do { unless ( stat ) { print "$File::Find::name: dead" }}}, "/";'

    However, it requires File::Find module of Perl. Any other simple one line solution?
    0
     
    LVL 38

    Expert Comment

    by:yuzh
    Here's a oneline solution:

    find /dir -type l | xargs cat > /dev/null

    Should throw an error for the ones that are broken on the screen.
    0
     
    LVL 38

    Author Comment

    by:wesly_chen
    > find /dir -type l | xargs cat > /dev/null
    Sound good.
    But how can "rm" the dead soft link automatically? (Of cause, "rm -i" to comfirm removal)
    0
     
    LVL 38

    Expert Comment

    by:yuzh
    Or (test on Solaris only!)

    (find /dir -type l | xargs cat > /dev/null)2>&1  | awk '{print $4}'

    will print all the broken link file names only
    0
     
    LVL 38

    Assisted Solution

    by:yuzh
    to rm them:

    rm `(find /dir -type l | xargs cat > /dev/null)2>&1  | awk '{print $4}'  `
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
    I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    909 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

    12 Experts available now in Live!

    Get 1:1 Help Now