Results 1 to 7 of 7
Hi,
I know how to know if a file or dir is a symbolic link. But how can I know that the target does NOT exist ?
Ex :
ln1 ...
- 05-19-2007 #1Just Joined!
- Join Date
- Oct 2006
- Posts
- 29
How to find "dead" symbolic links ?
Hi,
I know how to know if a file or dir is a symbolic link. But how can I know that the target does NOT exist ?
Ex :
ln1 -> tutu
ln2 -> titi
titi does not exist, tutu does. tutu and toto can be either dir or file. How can I make the difference between the 2 symbolic links ? I would like to use a tcsh script with simple commands such as ls, find ...
I think it is possible with find but I did not succeed to make it work.
I had another idea which was :
if the target is a dir and that I try to "cd" inside but can't, then the link is dead. But how to do if the target is a file ? Moreover, doing "cd" in each possible symbolic link and going back to current dir can be very long. This is a possible workaround but not a good idea.
Regards.
Eric.
- 05-19-2007 #2
file /symbolic/link/
If it's broken it'll say something along the lines of:
/usr/bin/ear: broken symbolic link to `/usr/lib/erlang/bin/ear'
This searches my system for them:Make sure you check it yourself instead of piping it to something else - a symbolic link with broken in the name will show too.Code:for i in `find /`; do if (test -h $i); then file $i|grep broken; fi; done
- 05-19-2007 #3Just Joined!
- Join Date
- Oct 2006
- Posts
- 29
Hi,
I did not know this command. I think it will help me a lot.
Thank you.
Eric.
- 05-20-2007 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:find / -type l ! -exec test -r {} \; -print
- 05-20-2007 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Observations: these two methods differ in two ways. As noted, the first may produce false positives if the pathname contains the string "broken". The first is also much slower. I benchmarked the two on a system of about 12K directories and 120K files:
Except for the anomalies mentioned above, both produced the same results. The method posted by ghostdog74 is far faster ... cheers, drlCode:real 5m24.875s user 0m22.586s sys 4m45.621s real 0m19.110s user 0m0.893s sys 0m2.875s
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 05-20-2007 #6Just Joined!
- Join Date
- Oct 2006
- Posts
- 29
Hi,
Thank you to all of you. I'll try both of them. This will help me to understand these options of the find command that I didn't know.
Regards.
Eric.
- 05-02-2010 #7Just Joined!
- Join Date
- Apr 2008
- Posts
- 3
Use the find command
The following command finds only broken symbolic links:
find -L -type l
This works because "-L" causes find to dereference symbolic links, in which case only broken links will have type "l". This is probably unique to gnu find (from findutils)
more info:
ynform.org slash w slash Pub slash UnixCommandEquivalents


