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

x
?
Solved

Search for the dead symbolic link

Posted on 2004-10-28
10
Medium Priority
?
1,432 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
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
  • 5
  • 3
  • 2
10 Comments
 
LVL 6

Expert Comment

by:blkline
ID: 12439007
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
ID: 12439163
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:
blkline earned 800 total points
ID: 12439339
Change:

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

to:

if [ -a $referenced ]; then

if you want to simply know if the file exists.  
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 38

Expert Comment

by:yuzh
ID: 12440759
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
ID: 12440815
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
ID: 12441553
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
ID: 12441847
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
ID: 12441895
> 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
ID: 12441975
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
yuzh earned 1200 total points
ID: 12441979
to rm them:

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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

618 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