1748280 Members
4026 Online
108761 Solutions
New Discussion юеВ

Re: Dead soft links

 
Bolek Mynarski
Frequent Advisor

Dead soft links

I have a number of directorries with rather a big number of soft links in them. What would be the command to find out which soft links are dead? I tried man on ln but nothing usefull came up.

I don't want to do it pen and paper way because it would defeat the purpose of having the computer. If a script has to be written, I can do it to as long as I know what I'm looking for... Thanks.
It'snever too late to learn new things...
9 REPLIES 9
Tim Malnati
Honored Contributor

Re: Dead soft links

No specific command available that I know of.

If you want to write a script, here are some thing to watch for:

Links can be associated with their target in one of three ways. It can point to an absolute path, a relative path (does not have to be in same directory, you see ../../ a lot in some unix implementations), or a direct reference to another file or directory in the same directory where the link exists.

A find command will locate all links on a system with the -type l search criteria.

There are some situations where the link may point to something that is only valid when an NFS mount is active.

Be sure to test your script VERY carefully prior to removing any links! Run the output to a file you review first! And don't rely on the script test operators for regular files and directories alone! If the link points to something like a block device, the test would fail and you could end up removing a valid link that is otherwise allowing the machine to run!

There is a good chance that someone out there has already written a script to do what you are looking for. In cases like these I usually will look around the open source community for something that fits the bill. A good source for stuff like this is http://www.oase-shareware.org/shell/links (shelldorado).

My best advice is to just leave them alone for now. The amount of disk space and inode usage is usually not worth talking about. I know some may suggest this goes against the motherhood of keeping a clean system. But I guess a clean system is not much use if its dead.
Dale McNamara
Frequent Advisor

Re: Dead soft links

From what I understand of your question, you want to find out which files are soft linked to non-existant files?

A rather slow way of searching for all files with symbolic links would be using the ncheck command to check for the inode of a file. The ncheck is a very slow command though. You can restrict it to just one mounted file system though.

You can get the inode number for a file with the ls -li command, then run;

ncheck [/dev/vg00/lvol1] | grep the_inode_number
Alan Riggs
Honored Contributor

Re: Dead soft links

ll | grep '->'|awk '{print $9" "$11}'| while read LINK FILE
do
if [ ! -a $FILE ]
then
echo $LINK is a dead link
fi
done
Bolek Mynarski
Frequent Advisor

Re: Dead soft links

Thanks everybody for a reply. Since I posted that message, I wrote a shell script myself that would search for that links (I was in the crunch and lucky for me, I was dealing on ly with files in specific directories which I knew were soft links to other files. To be more specific, they were links to database files). Again, I did not have to remove anything but only to print a list of links going nowhere. I have accomplished that task so that's done.

Another happy customer was out there.. :-)

Thanks again.
It'snever too late to learn new things...
Bolek Mynarski
Frequent Advisor

Re: Dead soft links

To Alan Riggs (or anybody who knows the answer)

O.K. I can only imagine what "-a" test stands for. I was looking for it in my books and documentation but could not find anything that would confirm what I think it.

So, what "-a" stands for in this particular context??

if [ ! -a $FILE ]

Thanks.
It'snever too late to learn new things...
Paul Hite
Trusted Contributor

Re: Dead soft links

If you are using the posix shell or ksh, then test (and [ ) are built-ins with more power than is shown on the man page for test. The man page for ksh says that
-a file true if file exists
Bolek Mynarski
Frequent Advisor

Re: Dead soft links

Hmm, it's all different then, from what I know for checking if the file exists:

-e or -f. I gues, I have to commit to my memory one more test paramter... :-)
It'snever too late to learn new things...
Alan Riggs
Honored Contributor

Re: Dead soft links

Paul got it. -a specifies "file exists" with no test for other traits (readable, directory, character special, etc.) is posix and ksh. Do a man ksh to see the full range of flags.
Tim Nelson
Honored Contributor

Re: Dead soft links

Try someting like this.
find ./ -type l -exec file {} ;

the file command will fail on any non-existant link files and should get you started. The list of errors could then be used for arguments into rm .