0
$ ln fun fun-hard
$ ln fun dir1/fun-hard
$ ln fun dir2/fun-hard
$ ls -1
total 16
drwxrwxr-x 2 me 4096 2018-01-14 16:17 dir1
drwxrwxr-x 2 me 4096 2018-01-14 16:17 dir2
-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun
-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun-hard

So there are 4 instances of the file fun Both the second fields in the listings for fun and fun-hard contains a 4, which now is the number of hard links that now exist for the file.

drwxrwxr-x 2 me 4096 2018-01-14 16:17 dir1
drwxrwxr-x 2 me 4096 2018-01-14 16:17 dir2

Why there’s 2 instances of the file fun-hard on dir1 and dir2? Isn’t there’s only one hard link: fun-hard?

-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun
-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun-hard

Can you explain more these 4 instances of fun and fun-hard, why they are repeated?

There are 2 hard links on dir1 and dir2, if…:

-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun
-rw-r—-r—- 4 me 1650 2018-01-10 16:33 fun-hard

…there are 4 instances of hard links why dir1 and dir2 are not 4 instances also?

muru
  • 72,889
  • 3
    Why there’s 2 instances of the file fun-hard on dir1 and dir2? The number of hard links to the dirs have absolutely nothing to do with the files they contain. – tkausl Jan 23 '24 at 23:31

2 Answers2

3

Most directories have at least two hardlinks to them; often there are three or more. If you execute the following two commands, you will see that there are three hard links to foo and they all, of course, have the same inode (because they are links to the same directory):

$ mkdir -p foo/bar
$ ls -ldi foo foo/. foo/bar/..

A directory generally will have at least the following hardlinks:

  • Itself (e. g. foo)
  • Its own link to itself (e. g. foo/.)
  • A link to itself from every subdirectory of which it is a parent (e. g. foo/bar/..), if any.

Those directories' hardlinks to themselves are utterly irrespective of their contents and are counted entirely separately from the counts of the hardlinks to each file in each directory.

DopeGhoti
  • 76,081
0

It may be easier to visualise this using more ls options, to show the inode numbers and the directory tree.

$ ls -l --inode --recursive
.:
total 16
6299664 drwxrwxr-x 2 paul paul 4096 Jan 24 10:42 dir1
6299832 drwxrwxr-x 2 paul paul 4096 Jan 24 10:42 dir2
6299812 -rw-rw-r-- 4 paul paul 1970 Jan 24 10:41 fun
6299812 -rw-rw-r-- 4 paul paul 1970 Jan 24 10:41 fun-hard

./dir1: total 4 6299812 -rw-rw-r-- 4 paul paul 1970 Jan 24 10:41 fun-hard

./dir2: total 4 6299812 -rw-rw-r-- 4 paul paul 1970 Jan 24 10:41 fun-hard $

The four entries marked 6299812 are different directory entries which all reference the same inode and its data blocks. If I change the modification time of one, and the contents of another, all four of them change -- there is only one copy of those things. In particular, there is no concept of which is the "original" -- they all are.

The inode holds a count of the hard links so the file manager knows when the link count goes to zero, at which point the data cannot be accessed and can be released back to the free space. But the inode does not record where those links originate. Directories, on the other hand, do hold a link to their direct parent.

Paul_Pedant
  • 8,679
  • if you add -a, you also see the link ./dir1/. pointing to the same directory as ./dir1 and perhaps explaining the link count of 2 there. (same with dir2) – ilkkachu Jan 24 '24 at 12:08