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?

| 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
  • See also How do I see what symlinks exist for a given directory? — that doesn't deal with multi-level symlinks, but the basic principle is the same (no, there's no method that doesn't involve an exhaustive search). – Gilles 'SO- stop being evil' Jun 14 '16 at 21:16

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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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