第三章Linux文件管理和IO重定向(2)-目录操作、元数据软硬链接

命令:tree mkdir rmdir  ln

1、查看目录树结构-tree

  • -a 显示所有文件和目录。
  • -d 显示目录名称而非内容。
  • -L指定显示的级层项目
[root@localhost litao]# tree /litao/
/litao/
├── logs
├── test
│   └── test1
│   └── test2
└── test1

5 directories, 0 files
[root@localhost litao]# tree -L 1 /litao/
/litao/
├── logs
├── test
└── test1

3 directories, 0 files
[root@localhost litao]# tree -L 2 /litao/
/litao/
├── logs
├── test
│   └── test1
└── test1

4 directories, 0 files
[root@localhost litao]# tree -a /litao/
/litao/
├── logs
├── test
│   └── test1
│   └── test2
└── test1

5 directories, 0 files

2、创建文件夹

  • -p 确保目录名称存在,不存在的就建一个。
  • -v 显示创建过程
[root@localhost litao]# mkdir log/logs
mkdir: cannot create directory ‘log/logs’: No such file or directory
[root@localhost litao]# mkdir -p log/logs

章节内容二:文件元数据及节点表结构

image

image

inode有上图中的左边区域组成,数据块由右边区域构成。不同的文件大小,通过多层级的间接指针协同完成。

直接块指针有12个,一个块大小为4KB,所以直接指针可以保存48KB的文件

间接块指针:每个指针占用4个字节,一个块是4KB,所以可以将一个块拆分成1024个指针,那么它的存储数据1024*4KB=4MB

双重间接块指针:同理可得它可以存储的数据为1024*4MB=4GB

三级指针可以储存文件数据大小为1024*4GB=4TB

当我们读写文件时底层的工作原理是什么?

image

访问一个文件的时候,先进入目录,目录中有相应的目录项,目录项中有对应的文件名和inode号,通过里面的指针指向相应的数据块,这样就访问到文件的内容了。目录这种“特殊的文件”,可以简单地理解为是一张表,这张表里面存放了属于该目录的文件的文件名,以及所匹配的inode编号,它本身的数据也放在数据区中;读写一个文件时就这样来回的从inode和数据区之间切换。可以把文件比作一本书,inode相当于书的目录,数据区相当于书的内容,读书时得先查目录,这本书呢又放在读书馆的书架上,书架可以理解为是目录,看书前先查书架子的索引。

image

image

本例子说明,创建硬链接inode数不变,链接数增加1 文件夹不支持硬链接

[root@localhost litao]# echo "hello word" > helloword [root@localhost litao]# ls -li helloword 134 -rw-r--r--. 1 root root 11 Dec 24 10:58 helloword //inode134 链接数 1 [root@localhost litao]# ln helloword hellowordlink [root@localhost litao]# ls -li helloword 134 –rw-r--r--. 2 root root 11 Dec 24 10:58 helloword //inode134 链接数 2


[root@localhost litao]# ln hello test
ln: hello: hard link not allowed for directory

(ln: hello:目录不允许硬链接)

[root@localhost litao]# ln helloword /boot/hello ln: failed to create hard link '/boot/hello' => 'helloword': Invalid cross-device link
(ln: failed to create hard link '/boot/hello' => 'helloword':无效的跨设备链接)

image

posted @ 2020-12-25 00:15  湘北10#  阅读(86)  评论(0编辑  收藏  举报