Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

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

up vote 1 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?

share|improve this answer

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.

share|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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .