When using ls -l
, I noticed that directories start with 2 hard links, and gain one for each subdirectory. I understand that the current directory link .
counts as one link and the parent directory link ..
of each subdirectory counts as a hard link, but:
Why don't subfiles count towards hard links when subdirectories do?
Why does the parent directory link ..
count as a hard link for both this directory and the parent directory?
Assume the following directory tree
/a
/b
/c
/d
Then directory b
at path /a/b
has 4 hard links. The links are the following:
b
(from the parent directory).
(from the directory itself)c/..
d/..
Files don't count against the link count of a directory, because files do not have a nested ..
directory, thus do not point back to the directory.
We can calculate the number of hardlinks for each directory shown above:
a
(from /
).
b/..
c
(from b).
d
(from b).