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?