Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I have a specific file in a UNIX filesystem, is there a way to use bash to find all links to that file, both symbolic and hard? If I need different commands for each, what are they?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Try this with GNU find:

find /start/dir -L -samefile /file/to/check -exec ls -li {} \;

Example output:

1234704 -rw-r--r-- 2 user1 user1 1134 2009-09-11 11:12 ./x1
1234704 -rw-r--r-- 2 user1 user1 1134 2009-09-11 11:12 ./x2
1234983 lrwxrwxrwx 1 user1 user1 2 2009-10-31 16:56 ./testx -> x1
2345059 lrwxrwxrwx 1 user2 user2 2 2010-01-03 16:17 ./x3 -> x1

You could use -ls instead of -exec but it will show the inode and other information of the target file instead of the individual files.

share|improve this answer
    
This output is showing hard and sym links? Sym links indicated by -> arrow? –  aaaidan Mar 16 '10 at 2:13
    
@aaaidan: Yes, that's correct. The inode number of x1 and x2 are the same indicating that they are hard links. The arrow indicates a symlink. Using -L and -samefile causes both to be listed. If you omit -L then symbolic links are not followed (see man find). –  Dennis Williamson Mar 16 '10 at 2:45

If you don't have GNU Find you can do this:

find / -inum "$(ls -i /file/to/check | cut -d ' ' -f 1)"

But it won't work for symbolic links.

share|improve this answer
1  
Oh I see now that this question already answers this: superuser.com/questions/12972/to-see-hardlinks-by-ls –  Ollie Saunders Jun 7 '10 at 13:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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