Why is the number of hard links 2 after Linux creates a directory

Execute ll in a directory

[hadoop@hadoop0 test]$ ll
total 8
drwxrwxr-x. 2 hadoop hadoop 4096 Jan 17 22:28 a
drwxrwxr-x. 2 hadoop hadoop 4096 Jan 17 22:28 b
-rw-rw-r--. 1 hadoop hadoop    0 Jan 17 22:28 c

It is found that the number of hard links between directory a and directory b is 2, while that of file c is 1
Why is that?

Discovery:
Under directory a, create a directory, and add 1 to the hard link of A

cd a ; mkdir d

[hadoop@hadoop0 test]$ tree
.
├── a
│   └── d
├── b
└── c

3 directories, 1 file

ll

[hadoop@hadoop0 test]$ ll
total 8
drwxrwxr-x. 3 hadoop hadoop 4096 Jan 17 22:29 a
drwxrwxr-x. 2 hadoop hadoop 4096 Jan 17 22:28 b
-rw-rw-r--. 1 hadoop hadoop    0 Jan 17 22:28 c

Create another

cd a ; mkdir e

[hadoop@hadoop0 test]$ tree
.
├── a
│   ├── d
│   └── e
├── b
└── c

4 directories, 1 file

ll

total 8
drwxrwxr-x. 4 hadoop hadoop 4096 Jan 17 22:32 a
drwxrwxr-x. 2 hadoop hadoop 4096 Jan 17 22:28 b
-rw-rw-r--. 1 hadoop hadoop    0 Jan 17 22:28 c

Found that a's hard link increased by 1

reason:
It's because of the two directories

ls -la

[hadoop@hadoop0 test]$ ls -la
total 16
drwxrwxr-x. 4 hadoop hadoop 4096 Jan 17 22:28 .
drwxrwxr-x. 3 hadoop hadoop 4096 Jan 17 22:28 ..
drwxrwxr-x. 4 hadoop hadoop 4096 Jan 17 22:32 a
drwxrwxr-x. 2 hadoop hadoop 4096 Jan 17 22:28 b
-rw-rw-r--. 1 hadoop hadoop    0 Jan 17 22:28 c

Every time a directory is created, the hard links of the parent directory and the current directory will be added by default
Using the stat command to view the file information, we found that:

stat a

[hadoop@hadoop0 test]$ stat a
  File: 'a'
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 135810      Links: 4
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  hadoop)   Gid: ( 1001/  hadoop)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2018-01-17 22:32:03.796604932 +0800
Modify: 2018-01-17 22:32:01.830052167 +0800
Change: 2018-01-17 22:32:01.830052167 +0800
 Birth: -

a's Inode is 135810

Enter directory a cd a
Execute stat

[hadoop@hadoop0 a]$ stat .
  File: '.'
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 135810      Links: 4
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  hadoop)   Gid: ( 1001/  hadoop)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2018-01-17 22:32:03.796604932 +0800
Modify: 2018-01-17 22:32:01.830052167 +0800
Change: 2018-01-17 22:32:01.830052167 +0800
 Birth: -

. the Inode of the directory is also 135810

Let's go to directory d (a's subdirectory) and use stat

[hadoop@hadoop0 d]$ stat ..
  File: '..'
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 135810      Links: 4
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  hadoop)   Gid: ( 1001/  hadoop)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2018-01-17 22:32:03.796604932 +0800
Modify: 2018-01-17 22:32:01.830052167 +0800
Change: 2018-01-17 22:32:01.830052167 +0800
 Birth: -

It is found that the Inode of the.. directory in the subdirectory d of a is the same as that of the a directory and the. Directory in the a directory

That's why when creating a directory, the number of hard links at the beginning is 2 (the name of the directory created and the... In the directory), and when creating a new directory, the hard links of the parent directory will be increased by 1 (the

Keywords: Hadoop

Added by Accurax on Sun, 03 May 2020 22:44:32 +0300