Search for the dead symbolic link

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.
LVL 38
wesly_chenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blklineCommented:
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
blklineCommented:
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
blklineCommented:
Change:

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

to:

if [ -a $referenced ]; then

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

yuzhCommented:
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
yuzhCommented:
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
wesly_chenAuthor Commented:
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
yuzhCommented:
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
wesly_chenAuthor Commented:
> 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
yuzhCommented:
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
yuzhCommented:
to rm them:

rm `(find /dir -type l | xargs cat > /dev/null)2>&1  | awk '{print $4}'  `
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.