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?
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 /usrreference 2 is
.
in directory /usr/binIf you consider /usr/ the number of reference is 3 .
reference 1 is
/usr
in directory /reference 2 is
.
in directory /usrreference 3 is
..
in directory /usr/binIf you consider /var/cache/man/ the number of reference is 26 .
reference 1 is
man
in directory /var/cachereference 2 is
.
in directory /var/cache/manreference 3 is
..
in directory /var/cache/man/csreference 4 is
..
in directory /var/cache/man/dareference 5 is
..
in directory /var/cache/man/es../..