45

How can we find all hard links to a given file? I.e., find all other hard links to the same file, given a hard link?

Does filesystem keep track of the hard links to a file?

The inode of a file only stores the number of hard links to the file, but not the hard links, right?

51

If the given file is called /path/to/file and you want to find all hard links to it that exist under the current directory, then use:

find . -samefile /path/to/file 

The above was tested on GNU find. Although -samefile is not POSIX, it is also supported by Mac OSX find and FreeBSD find.

Documentation

From GNU man find:

-samefile name
       File refers to the same inode as name. When -L is in effect, this can include symbolic links.

| improve this answer | |
  • 1
    The result of this query is incomplete for users lacking privileges on the directories that contain the hard links. – Aaron Brick Jan 16 '18 at 6:55
  • 5
    Of course. You're listing entries in directories. You need read access to those directories in order to list them. – reinierpost Jan 30 '18 at 17:01

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