6

A question for ls command.

root@cqcloud script]# ls /var/www/html -la 
total 36 
drwxr-xr-x  9 root root 4096 Aug 31 01:12 .
drwxr-xr-x  7 root root 4096 Aug 31 01:10 ..
drwxr-xr-x  2 root root 4096 Aug 26 04:07 cmd
drwxr-xr-x  5 root root 4096 Jul  3 10:07 cn.fnmili.com
drwxr-xr-x  7 root root 4096 Aug 30 11:42 internal
drwxr-xr-x  3 root root 4096 Jul 25 02:03 node
drwxr-xr-x  4 root root 4096 Jul 11 01:26 sandbox
drwxr-xr-x 13 root root 4096 Aug 26 03:45 tpshop
drwxr-xr-x  2 root root 4096 Aug 31 01:12 trash

[root@cqcloud script]# ls /var/www/html/cmd -la 
total 16 
drwxr-xr-x 2 root root 4096 Aug 26 04:07 . 
drwxr-xr-x 9 root root 4096 Aug 31 01:12 .. 
-rw-r--r-- 1 root root 52 Aug 26 04:07 .htaccess 
-rw-r--r-- 1 root root 73 Aug 26 04:02 df.php

You can see that the cmd folder has a link count of 2, but it actually has 4 links, including 2 files, . and .. folder. Can anyone explain why?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • Could you confirm whether /var/www/html has only one subdirectory? – Kusalananda Aug 30 '17 at 18:05
  • Also: https://unix.stackexchange.com/questions/101515/why-does-a-new-directory-have-a-hard-link-count-of-2-before-anything-is-added-to – muru Aug 31 '17 at 01:49

1 Answers1

10

The link count for a directory is the number of names that directory has (this works just as for regular files).

Your cmd directory has two names:

  1. cmd in its parent directory.
  2. . in the directory itself.

The /var/www/html directory has nine names:

  1. html in its parent directory.
  2. . in itself.
  3. .. in each of its (seven) subdirectories.

Under normal circumstances, the link count for a directory's . entry should be 2 plus the number of subdirectories that it contains.


This is also true for the root directory /, even though it does not have a parent directory and therefore ought to have a link count of 1 plus the number of subdirectories.

What it does have is a .. directory, which takes you back to /. So that solves that riddle; it's /.. that provides the "extra" link to /. This is the only directory whose .. directory is a link back to ..

Kusalananda
  • 333,661
  • Ext4 FAQ explain it ( https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout ). Note that 1 has a special meaning. – Franklin Piat Aug 30 '17 at 18:10
  • Can you explain why folder internal has 7 links but folder cmd has 2 links over here? – Weijing Lin Aug 30 '17 at 19:58
  • @WeijingLin I'm guessing that internal has five subdirectories. It has a link count of 7 since each of its five subdirectory contains .. that acts like "names" (links) to the internal directory. It also has an entry in /var/www/html and in itself (.). That makes 7 in total. The cmd directory, as I wrote above, only has two links; in its parent directory (the link is called cmd), and in itself (.). It contains no subdirectories, so it only has two links. Any directory without subdirectories will have a link count of 2. – Kusalananda Aug 30 '17 at 20:05
  • 1
    @WeijingLin By the way, this is in no way "a noob question" :-) – Kusalananda Aug 30 '17 at 20:07
  • @Kusalananda, thanks, I realized that "name"/"link" actually stands for folder/directory, exclude file ~~~ – Weijing Lin Aug 31 '17 at 00:29