7
$ sudo su
# dd if=/dev/zero of=./myext.img bs=1024 count=100
.
.
.
# modprobe loop
# losetup --find --show myext.img
/dev/loop0
# mkfs -t myext /dev/loop0
.
.
.
# mkdir mnt
# mount /dev/loop0 ./mnt
# cd mnt
# ls -al
total 17
drwxr-xr-x  3 root root  1024 Jul 21 02:22 .
drwxr-xr-x  11 shisui shisui  4096 Jul 21 02:22 ..
drwx------  2 root root 12288 Jul 21 02:22 lost+found

(Cut out some of the output of some commands). My first question is, why isn't mnt showing up in the ls -al output? All I see is root. I cd'd into \mnt so I expected to see it in my ls -al output.

But then what is the third link?

Finally, are all the link numbers in this ls -al output hard links? Or does this link count also include symbolic links?

4
  • 1
    root is the owner. The number of hard links is shown, that is, the number of references to that folder from other folders. A simple file would have 1 link/reference. The background is that only when that number reaches 0 the space on the disk will be de-allocated (made available for other files). Commented Dec 12, 2021 at 8:58
  • 2
    @Ned64 not quite: the space is freed when the count reaches 0 and all open descriptions are closed (which I imagine you’re aware of, I just think it’s worth mentioning). Commented Dec 12, 2021 at 9:18
  • @StephenKitt Good point, yes. Commented Dec 12, 2021 at 11:55
  • Does this answer your question? Why does a new directory have a hard link count of 2 before anything is added to it? Commented Dec 13, 2021 at 1:27

1 Answer 1

27

You don’t see mnt in the ls -al output because you’re inside mnt; it is represented by .

There’s another hard link to ., lost+found/..; this explains the count of 3 links to the directory:

  1. . which points to the directory itself;
  2. .. which also points to the directory, because it’s the root directory in the file system (see Why does a new directory have a hard link count of 2 before anything is added to it?);
  3. lost+found/.., which points back to the root directory (again, in the file system, so mnt here).

The link counts shown by ls -l count hard links only; symlinks aren’t included.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.