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

2 Answers2

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
Jens
  • 1,752
  • 4
  • 18
  • 36
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. –  Dec 16 '15 at 21:58