0

Is there a way to list all hardlinks, by path/filename, or even just by file name alone, that point to a given inode? I have a file that ls -al reports has three hardlinks, but it should only have two. How do I go about finding the rogue hardlink?

I found this other post: How to find all hard links to a given file?

But it only finds file named the same. These hardlinks will/may not be named the same.

5
  • 1
    What command did you use that gives only files with the same name? The answer there uses find -samefile, and touch foo1; ln foo1 foo2; find . -samefile foo1, lists ./foo1 and ./foo2, isn't that what you want?
    – ilkkachu
    Commented Dec 19, 2022 at 20:54
  • 1
    in any case, due to the filesystem structure, the only way to find those links is to read through the whole directory tree
    – ilkkachu
    Commented Dec 19, 2022 at 21:12
  • 1
    Remember that all hard links are ranked equally. There is not an "original" file and two other "hard links" to it. There is one inode number which is a unique identifier for the file-system/inode and owns the data blocks and the stat information. There are three entries in various directories which can have any unique full pathname you like, but they all reference the same inode. I also think you misunderstand the -samefile option. You give the name of any one of the hard links, and find gets its inode reference and then searches for all directory entries that refer to the same inode. Commented Dec 19, 2022 at 21:57
  • @larsks This might do the trick! Thanks.
    – DasKraut
    Commented Dec 20, 2022 at 5:01
  • @Paul_Pedant yeah, I definitely don't know what it does. This was my first attempt at trying to figure out what this mysterious third hardlink is. If I can figure out how to scan the parent folder for everything that matches its inode number, maybe then I can answer the mystery. When I ran the find command with the -samefile option, it only showed me the one hardlink. But I admittedly am very newbish when it comes to the find command. It never seems to work how I expect. That's on me, being a hobbyist and not a professional though.
    – DasKraut
    Commented Dec 20, 2022 at 5:05

1 Answer 1

2

The linked files may be anywhere in the file system, not just in a direct parent. This will report all files with three or more hard links in your home directory, grouped by inode. You may have spotted one example, but there may be others of interest:

find ~ -type f -links +2 -printf 'inode %i links %n name %p\n' | sort -n

I don't have any 3-ways, but this is my test for global two-way hard links.

$ find ~ -type f -links +1 -printf 'inode %i links %n name %p\n' | sort -n
find: ‘/home/paul/.gvfs’: Permission denied
find: ‘/home/paul/.cache/dconf’: Permission denied
inode 6173828 links 2 name /home/paul/Calhoun.post
inode 6173828 links 2 name /home/paul/SandBox/JpgTool/Calhoun.again
inode 6296398 links 2 name /home/paul/SandBox/fileGroup/fileGroup.V05
inode 6296398 links 2 name /home/paul/SandBox/fileGroup/myHardLink
3
  • You'd want to run find on the mountpoint and add the -xdev predicate. Note that find may miss some if there are directories it doesn't have search of read permission for of if there's another fs mounted on some non-empty directory masking its contents as a result. Commented Dec 20, 2022 at 11:31
  • @StéphaneChazelas Unclear about -xdev as hard links cannot be cross-device. The %D output would also be needed, to differentiate the file system. But yes, I am assuming the OP's problem has no edge cases. Commented Dec 20, 2022 at 12:09
  • 1
    Would be worth noting that -printf is GNU-specific. Commented Dec 20, 2022 at 12:28

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