Is there a command that I could execute to find out if a directory is referenced by a symlink somewhere on the machine.

ie:

pwd =>  /home/user/source/

vdir 
lrwxrwxrwx 1 user group   24 2011-04-08 10:59 Symlink -> /home/user/source/Test
drwxr-xr-x 2 user group 4096 2011-04-08 10:59 Test

Using this exemple, I want to know where to find Symlink because it reference Test.

I use Ubuntu 10.04.

link|improve this question
feedback

migrated from stackoverflow.com Apr 10 '11 at 8:49

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 0 down vote accepted

Maybe you could try something like:

sudo find / -lname /path/to/your/file

But beware that some links can be defined with relative paths.

Or you can list all symbolic links and grep for your file using:

sudo find / -type l | grep /path/to/your/file

(the "l" in "-type l" is not the number one but L)

But each method has some drawbacks. Maybe someone knows better?

link|improve this answer
feedback

You have to scan the entire filesystem for softlinks, then resolve them and see if they point to the same location. Don't try to do a text-compare of the link destination, since that could go through another hard or softlink; you need to compare inodes probably.

link|improve this answer
@jesup, Do you have any example of commands, to do this. I am starting to discover the benefits of the command line. thanks – yvoyer Apr 8 '11 at 15:24
I'd start with 'find -L / -xtype l -samefile Test' That may do it right there – jesup Apr 8 '11 at 16:02
I'd start with 'find -L / -xtype l -samefile Test' --- That may do it right there (find is powerful). I was going to get a list of all links with 'find / -type l' and pipe it to xargs to test against the inode number of Test, but the first version seems to do it all. – jesup Apr 8 '11 at 16:08
@jesup, find -L / -xtype l -samefile Test works perfectly thanks. But I had to call it with sudo. – yvoyer Apr 8 '11 at 18:28
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.