0
gui@Latitude:~$ cd playground
gui@Latitude:~/playground$ ls -l
total 8
drwxrwxr-x 2 gut gut 4096 set 20 16:18 dir1
-rw-r--r-- 1 gut gut 2903 set 20 14:46 gato
gui@Latitude:~/playground $ ln gato gato-rato
gui@Latitude:~/playground $ ls -l
total 12
drwxrwxr-x 2 gui gui 4096 set 20 16:18 dir1
-rw-r--r-- 2 gui gui 2903 set 20 14:46 gato
-rw-r--r-- 2 gui gui 2903 set 20 14:46 gato-rato
gui@Latitude:~/playground $

Please clarify this for me:

drwxrwxr-x 2 gut gut 4096 set 20 16:18 dir1

👆🏼 dir1 has 2 hard links, right? : . inside the dir1, and dir1?

rw-r--r-- 1 gui gui 2903 set 20 14:46 gato

At the start, this one has one 👆🏼 has one link, right? (hard link?)

-rw-r--r-- 2 gui gui 2903 set 20 14:46 gato-rato

I have created the hard link gato-rato. Now, there are 2 links for gato and 2 for gato-rato, which I find confusing. Is this because there are 2 links for both now? That’s: gato gato rato and gato-rato gato.

1

2 Answers 2

3

I think you're thinking of it a bit wrong. gato and gato-rato are not two different (but linked) files, they are two names for the same file. Those names -- or more technically, directory entries -- are the "links" that're being counted.

It might help to use ls -li to see the inode numbers, which are basically the file ID numbers on the volume. Here's a recreation on my system:

Gordons-MBP:playground gordon$ ls -li
total 8
12931424970 drwxr-xr-x  2 gordon  staff  64 Sep 20 21:38 dir1
12931424987 -rw-r--r--  1 gordon  staff   7 Sep 20 21:39 gato
Gordons-MBP:playground gordon$ ln gato gato-rato
Gordons-MBP:playground gordon$ ls -li
total 16
12931424970 drwxr-xr-x  2 gordon  staff  64 Sep 20 21:38 dir1
12931424987 -rw-r--r--  2 gordon  staff   7 Sep 20 21:39 gato
12931424987 -rw-r--r--  2 gordon  staff   7 Sep 20 21:39 gato-rato

You can see that the "gato" and "gato-rato" directory entries both link to inode #12931424987. That inode has two directory entries linking to it, and therefore has a link count of 2.

Similarly, the subdirectory has a second link:

Gordons-MBP:playground gordon$ ls -lid dir1 dir1/.
12931424970 drwxr-xr-x  2 gordon  staff  64 Sep 20 21:38 dir1
12931424970 drwxr-xr-x  2 gordon  staff  64 Sep 20 21:38 dir1/.

So the "dir1" entry in the current directory and the "." entry in that directory itself are the two links to inode #12931424970.

1

Let's take an analogy.

Consider a book, containing chapters. In my book's alphabetical index I have an entry for the chapter "Cooking an egg". Each time I make an entry in this index I write a note at the top of the chapter to remind me how many entries I've got. Now I decide to make another entry in my index called "Egg, cooking" so that I can find the chapter not only under "C" for "Cooking" but also under "E" for "Egg". I also update the note at the top of the chapter itself to remind me I now have two entries in the index.

I still have only one chapter, but it has two entries in the index. Neither entry in the index is more right or more wrong; both are equally valid.

echo 'Recipe for cooking an egg' >'Cooking an egg'
ln 'Cooking an egg' 'Egg, cooking'

ls -lid Cooking*

You will see that the entry is telling you that there are two links to the file. This command also provides the inode number, which in our example could have been the page number in our book.

ls -lid Egg*

Here you get the same information, because it's simply another reference to the same file (the same chapter in the book). It also tells you there are two links to the file. Remember, in our analogy the number of index entries is kept with the chapter, and it's the same here: the number of links is kept with the file, not the file name.

Importantly, you can delete either file name and the file will still exist. In our analogy that's like erasing an entry from our index. The chapter still exists until the number of entries in the index reaches zero - and then we throw it away.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .