-1

The Linux Command Line

William Shotts

Page 33

[me@linuxbox playground] $ ls -1   
total 16    
drwxrwxr-x 2 me me 4096 2018-01-14 16:17 dir1
drwxrwxr-x 2 me me 4096 2018-01-14 16:17 dir2
-rw-r—r— 4 me me 1650 2018-01-10 16:33 fun
-rw-r—r— 4 me me 1650 2018-01-10 16:33 fun-hard

One thing we notice is that both the second fields in the listings for fun and fun-hard contain a 4, which is the number of hard links that now exist for the file. Remember that a file will always have at least one link because the file's name is created by a link.

Please explain this better:

fun and fun-hard contain 4 hard links?dir1 and dir2 contain 2 hard links?

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

How can this data file contain 2 hard links ?

Remember that a file will always have at least one link because the file's name is created by a link.

I can’t understand this

unxnut
  • 6,008

2 Answers2

1

When you mkdir dir1, it creates the directory and populates it with . which refers to current directory in dir1. So, there are two links: dir1 in the directory where you created dir1 and . inside dir1.

unxnut
  • 6,008
1

Please explain this better: fun and fun-hard contain 4 hard links?dir1 and dir2 contain 2 hard links?

The book's remark, "which is the number of hard links that now exist for the file", hints that the multiple links are a result of several consecutive lessons in the book, showing how to create hard links. So the book's larger context will explain why there are 4 of them, and this remark mentions why a file will always show at least one hard link.

Explanations for the book's examples and descriptions are best looked-for in the book, especially one that's written as a learning guide that begins with basic concepts and progresses to the more sophisticated concepts/examples, like this one.

As other answers mentioned, a directory (which is different from a file) begins life with two hard links instead of one.

Sotto Voce
  • 4,131