1

When we create a directory in UNIX it has 2 hard links: . and ... One to itself and another one to its parent directory.

As I read the . increases counter of the directory, and the .. increases hard links counter of parent directory.

So only . is counted in the directory, but there are still 2 hard links when I do ls -l. What is the second one? Can someone tell me how hard links are counted in directories? Imagine, we have this hierachy:

parent    - 4 hard links
.
. child_1 - 2 hard links
.
. child_2 - 2 hard links

marked as duplicate by muru, chaos, Gilles, jimmij, roaima Dec 23 '15 at 10:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Dec 23 '15 at 9:02

This question came from our site for professional and enthusiast programmers.

3

child1 has two links because of the child1 entry in the parent and the . entry in child1.

Note that parent in the described constellation usually has four links, not 3 because it is the child of some other parent (unless it's the root dir):

$ find parent | xargs stat -c "%h %n"  # %h=link count, %n=name
4 parent
2 parent/child2
2 parent/child1
  • On the extN filesystems even / has ... It's just that it points to itself. – roaima Dec 23 '15 at 10:27
0

Okay. That's what I got asking my lecturer:

The . increments by 1 the count for the directory itself. The .. increments by 1 the count of the parent directory. New child directories have 2 hard-links because the name of the directory is a hard-link as well linking to this directory.

  • I'd say this is pretty similar to what my answer describes. – Jens Dec 16 '15 at 15:19
  • Here a little difference that may be important. child1 has 2 hard links because tha name: child1 is a hard link too. Together with the . hard link it makes 2 hard-links totally. – PinkiNice Dec 16 '15 at 21:58