在一个目录下执行 ll
[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
发现,a目录与b目录的硬链接数是2 ,而文件c的硬链接数是1
这是为什么呢?
发现:
在a目录下,创建一个目录,a的硬链接会加1
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
再创建一个
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
发现a的硬链接又增加1.
原因:
原来是因为目录中的 ..
.
这两个目录
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
每创建一个目录,就会在创建的目录下默认新增..
上级目录的硬链接和.
当前目录的硬链接
使用stat命令查看文件的信息,我们发现:
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 的Inode 为 135810
进入a目录
cd a
执行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: -
.
目录的Inode也是135810
我们再进入d目录 (a的子目录),使用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: -
发现a的子目录d中的..
目录的Inode和a目录的Inode以及 a目录中的.
目录的Inode相同.
这就是为什么创建一个目录,一开始硬链接数为2( 创建的目录名和目录中的.
,并且新创建一个目录,父目录的硬链接会加1 (子目录中的..
)