Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can one get the list of all the aliases of a file (including of type directory) efficiently, given that a file:

  1. may have multiple hard links, multipe soft links, multi-level soft links
  2. may be present under a directory which is mounted at another location in the same filesystem
  3. may be present under a directory that has link(s) pointing to it; again the links having features mentioned in 1

Could a possible solution be using inodes?

share|improve this question
    
GNU find has the -samefile option, but find / is hardly efficient as it has to basically traverse the entire filesystem. –  jw013 May 3 '12 at 11:06
    
I suppose there might be a way to find the other hard links in some file system formats, but if you want soft links as well then I'm afraid it's brute force or nothing. –  ams May 3 '12 at 13:24

1 Answer 1

Hard links can be identified by comparing both the inode number and the device number (two different mounted file systems may each have a file with the same inode number, but they will have different device numbers). You can read the device/inode manually with stat $file, or find can do it for you with -samefile. You can make searching for hard links more efficient by searching only within the same device:

find $fsroot/. -mount -samefile $myfile

Soft links are harder as they can be located anywhere. You need to dereference them to identify the inode/device. You can do this manually with stat -L or automatically with find -L:

find -L / -samefile $myfile

Note that this will also find hard links. I don't think find has an easy way to search for only soft links that point to a specific inode.

It might be that some file-systems can tell you this information more efficiently, but I think the in the general case you need to scan every file.

share|improve this answer

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.