0

I am tracking down what may be a stray or erroneous hard link to a directory, so I want to find all the hard links to a directory to manually verify them. (fsck finds no errors, but the link count appears wrong on casual inspection.)

The normal way to check for hard links to a file is to use find, either find -inum or find -samefile, but neither of those work for directories:

user$ find . -links 3 -name '.*' -ls
user$ ls bar
l1/
user$ ls -iad bar
10541088 bar/
user$ ls -iad bar/.
10541088 bar/./
user$ ls -iad bar/l1/..
10541088 bar/l1/../
user$ find . -inum 10541088 -ls
10541088        0 drwxr-xr-x    3 user             admin                 102 Mar 17 11:36 ./bar
user$ find . -samefile bar -ls
10541088        0 drwxr-xr-x    3 user             admin                 102 Mar 17 11:36 ./bar

Note that although bar, bar/., and bar/l1/.. all are hard links to the same inode, find only lists one of them.

I can use ls -aiR / | grep 10541088 and that will at least find the dot and dot dot links, but I am not positive that there is not some other kind of hidden file that this will miss, plus ls will cross devices, which I do not want.

Is there a better way to find all the hard links to a directory?

Old Pro
  • 1,425
  • 12
  • 21
  • 1
    Current Linux filesystems do *not* allow hardlinks to directories... – shodanshok Mar 17 '19 at 19:41
  • @shodanshok All linux systems allow hardlinks to directories. As you can see from what I posted, the inode 10541088 represents a directory and it has 3 hardlinks to it. – Old Pro Mar 17 '19 at 19:45
  • 1
    No for any [remotely recent Linux distribution](https://askubuntu.com/questions/210741/why-are-hard-links-not-allowed-for-directories). Also check [ln manpage's](https://linux.die.net/man/1/ln) notes about `-F`. Can you show your operating system version? – shodanshok Mar 17 '19 at 20:12
  • 1
    No, hard links to directories are not possible, barring filesystem corruption. They are explicitly not allowed. Further, the output you pasted does not demonstrate the existence of a hard link. Rather, you have simply referenced the same directory three different times. – Michael Hampton Mar 17 '19 at 20:15
  • @MichaelHampton having different names referring to the same inode is the definition of having hard links. The names `.` and `..` are always hard links. And I am looking for filesystem corruption that `fsck` missed. – Old Pro Mar 17 '19 at 20:34
  • 1
    `.` and `..` do not really count. Is there something else? – Michael Hampton Mar 17 '19 at 20:38

1 Answers1

1

Having a count of 3 is correct , when you have one sub-directory .

If a directory have 5 sub-directories , you must have a count of 7 .

In this example i suppose that /usr/bin/ does not have a sub-directory

If you consider /usr/bin the number of reference is 2 .

  • reference 1 is bin in directory /usr

  • reference 2 is . in directory /usr/bin

If you consider /usr/ the number of reference is 3 .

  • reference 1 is /usr in directory /

  • reference 2 is . in directory /usr

  • reference 3 is .. in directory /usr/bin

If you consider /var/cache/man/ the number of reference is 26 .

  • reference 1 is man in directory /var/cache

  • reference 2 is . in directory /var/cache/man

  • reference 3 is .. in directory /var/cache/man/cs

  • reference 4 is .. in directory /var/cache/man/da

  • reference 5 is .. in directory /var/cache/man/es

  • ../..

EchoMike444
  • 449
  • 1
  • 3
  • 6
  • Yes, in the example I gave, the link count is correct. My point was that `find` would not establish that the link count is correct, it would find only 1 link. I'm looking for something better than `ls -R` to find all the links. – Old Pro Mar 20 '19 at 18:25
  • @OldPro , in fact links = 3 is not always true . – EchoMike444 Mar 21 '19 at 04:19
  • Why are you talking about how many links there should be? That has nothing to do with my question. – Old Pro Mar 22 '19 at 04:02