1.1 硬链接和软连接
-
硬链接(Hard Link)
#创建硬链接命令 ln filename hardLinkFileName
-
软链接(Symbel Link)
#创建软链接命令 ln -s filename softLinkFileName
1.2 创建软硬链接,查看区别
-
首先创建两个文件,查看Inode编号
#创建文件 root@Dog-li:/mnt/linux# echo hello1 > hello1 root@Dog-li:/mnt/linux# echo hello2 > hello2 root@Dog-li:/mnt/linux# ls -l total 8 -rw-r--r-- 1 root root 7 9月 12 10:47 hello1 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2 #查看文件明细 root@Dog-li:/mnt/linux# stat hello1 File: hello1 Size: 7 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 11 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-09-12 10:47:58.428265916 +0800 Modify: 2021-09-12 10:47:58.428265916 +0800 Change: 2021-09-12 10:47:58.428265916 +0800 Birth: - #查看文件对应的Inode编号,前面的数字表示Inode编号 root@Dog-li:/mnt/linux# ls -li total 8 11 -rw-r--r-- 1 root root 7 9月 12 10:47 hello1 12 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2
-
删除hello1后再次创建,查看Indoe编号变化
#删除hello1文件 root@Dog-li:/mnt/linux# rm hello1 root@Dog-li:/mnt/linux# ls -li total 4 12 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2 #再次创建 root@Dog-li:/mnt/linux# echo hello1 > hello1 root@Dog-li:/mnt/linux# ls -li total 8 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1 12 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2
从操作可以看出,原来的hello1文件的Indoe编号为11,当删除hello1文件时,Inode11被回收,当再次创建hello1文件时,给其分配的Inode编号还是11,由于/mnt/linux这个分区刚做初始化,所以文件的Inode编号从11开始,前面10个编号为保留编号
-
为hello1创建硬链接
#为hello1创建硬链接 root@Dog-li:/mnt/linux# ln hello1 hello1_hardlink root@Dog-li:/mnt/linux# ls -li total 12 11 -rw-r--r-- 2 root root 7 9月 12 10:53 hello1 11 -rw-r--r-- 2 root root 7 9月 12 10:53 hello1_hardlink 12 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2 #查看hello1的链接数 root@Dog-li:/mnt/linux# stat hello1 File: hello1 Size: 7 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 11 Links: 2 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-09-12 10:53:22.740667470 +0800 Modify: 2021-09-12 10:53:22.740667470 +0800 Change: 2021-09-12 10:57:56.636876364 +0800 Birth: - #查看hello1_hardlink的链接数 root@Dog-li:/mnt/linux# stat hello1_hardlink File: hello1_hardlink Size: 7 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 11 Links: 2 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-09-12 10:53:22.740667470 +0800 Modify: 2021-09-12 10:53:22.740667470 +0800 Change: 2021-09-12 10:57:56.636876364 +0800 Birth: -
为hello1创建一个硬链接之后,硬链接的Inode编号与原始文件的Indoe编号一样,同时Inode对应的文件链接数变为2,在《文件系统实现》中介绍了文件目录项和Inode的关系,hello1和hello1_hardlink对应两个目录项,但这两个目录项中的Indoe编号是一样的,所以删除其中任何一个文件,对另外一个文件都没有影响
#删除hello1,查看hello1_hardlink root@Dog-li:/mnt/linux# ls -l total 12 -rw-r--r-- 2 root root 7 9月 12 10:53 hello1 -rw-r--r-- 2 root root 7 9月 12 10:53 hello1_hardlink -rw-r--r-- 1 root root 7 9月 12 10:48 hello2 root@Dog-li:/mnt/linux# rm hello1 root@Dog-li:/mnt/linux# cat hello1_hardlink hello1
-
为hello2创建软连接
#为hello2创建软连接hello2_softlink root@Dog-li:/mnt/linux# ln -s hello2 hello2_softlink #查看软链接的Indoe编号,与原始文件不一样 root@Dog-li:/mnt/linux# ls -li total 8 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1_hardlink 12 -rw-r--r-- 1 root root 7 9月 12 10:48 hello2 13 lrwxrwxrwx 1 root root 6 9月 12 11:14 hello2_softlink -> hello2 #Inode对应的文件链接数也没有变化 root@Dog-li:/mnt/linux# stat hello2 File: hello2 Size: 7 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 12 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-09-12 10:48:06.624279034 +0800 Modify: 2021-09-12 10:48:06.624279034 +0800 Change: 2021-09-12 10:48:06.624279034 +0800 Birth: - #删除hello2文件,查看软链接 root@Dog-li:/mnt/linux# rm hello2 root@Dog-li:/mnt/linux# cat hello2_softlink #找不到该文件(指hello2) cat: hello2_softlink: No such file or directory root@Dog-li:/mnt/linux# ls -li total 4 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1_hardlink 13 lrwxrwxrwx 1 root root 6 9月 12 11:14 hello2_softlink -> hello2
删除原始文件之后,再查看软链接的内容发现找不到文件,意味着软链接对应的Inode里面,存放的不是原始数据的物理块地址,可能是原始文件hello2的信息
向软链接写入内容,看是什么效果
root@Dog-li:/mnt/linux# ls -li total 4 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1_hardlink 13 lrwxrwxrwx 1 root root 6 9月 12 11:14 hello2_softlink -> hello2 root@Dog-li:/mnt/linux# echo hello3 > hello3 root@Dog-li:/mnt/linux# ls -li total 8 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1_hardlink 13 lrwxrwxrwx 1 root root 6 9月 12 11:14 hello2_softlink -> hello2 12 -rw-r--r-- 1 root root 7 9月 12 11:25 hello3 root@Dog-li:/mnt/linux# echo this is a new content > hello2_softlink root@Dog-li:/mnt/linux# ls -li total 12 11 -rw-r--r-- 1 root root 7 9月 12 10:53 hello1_hardlink 14 -rw-r--r-- 1 root root 22 9月 12 11:26 hello2 13 lrwxrwxrwx 1 root root 6 9月 12 11:14 hello2_softlink -> hello2 12 -rw-r--r-- 1 root root 7 9月 12 11:25 hello3 root@Dog-li:/mnt/linux# cat hello2 this is a new content
先创建一个文件hello3,把原来hello2的Indoe编号12给占用了,然后再向软链接hello2_hardlink写入内容,发现又重新创建了一hello2的文件,文件内容也是写入到软链接的内容
由此可以推断,软链接的Indoe里面存放的是hello2文件的相关信息,当删除hello2后,向软链接写入内容时,其实是向原文件hello2写入内容,发现没有该文件,则先创建该文件
1.3 文件时间
创建一个test文件,查看文件对应的几个时间
root@Dog-li:/mnt/linux# touch test
root@Dog-li:/mnt/linux# stat test
File: test
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 16 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-09-12 11:36:00.722990877 +0800
Modify: 2021-09-12 11:36:00.722990877 +0800
Change: 2021-09-12 11:36:00.722990877 +0800
Birth: -
Access:对应最后一次访问文件的时间
Modify:最后一次修改文件内容的时间
Change:最后一次修改文件属性或文件内容的时间
Birth(Create):文件的创建时间
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153676.html