Is a directory removed when its number of hard links becomes 0

directory

Is a directory removed when its number of hard links becomes 0?

A directory always has at least 2 as its number of hard links, because of .. When rm -r a directory, does it decrease the number of hard link from 2 to 0 by 2 instead of 1?

Can the number of hard links of a directory ever be 1?

Thanks.

Best Answer

Firstly not all filesystems use . and .. as hard links. this is documented in the gnu find manual. I am going to ignore those filesystems for the rest of my answer because they were not designed for unix and only complicate things without adding clarity. I am also going to ignore the root directory and mount points for the same reason.

the number of links to a directory is never less than two because of . and ... The number of subdirectories is equal to the number of links minus two. Because of this you cannot link or unlink a directory, so rm -r will stat a file before deleting and use rmdir instead of unlink on directories. The two system calls use completely different code paths in the kernel.