1.10- 链接文件 及 硬链接和软连接的区别

如果需要在系统中维护同一个文件的两个或多个副本,不需要使用两个或多个物理副本,可以使用一个物理副本和多个虚拟副本,这种虚拟副本称为链接

链接是目录中的占位符,指向文件的真实位置(可以理解为指针)
Linux中有两种不同的文件链接类别。

  • 符号链接(又称软链接)
  • 硬链接

硬链接创建一个单独的文件,引用该硬链接文件的效果跟引用源文件一样:

1、创建硬链接的方式
方法一:cp -l 源文件 硬链接文件

[root@hadoop tmp]# cp -l test_file1 test_file4
[root@hadoop tmp]# ls -li test*
16811483 -rw-r--r--. 2 root root 7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月  10 18:56 test_file2
16811795 -rw-r--r--. 1 root root 7 8月  10 18:48 test_file3
16811483 -rw-r--r--. 2 root root 7 8月  10 18:48 test_file4

-l参数创建test_file1的硬链接文件
注意:
1> 硬链接文件和源文件的索引节点相同,硬链接文件一直维护索引节点,并保留数据
2> 硬链接文件和源文件的内容相同
3> 硬链接文件和源文件的文件大小相同
4> 硬链接文件和源文件的最后修改时间相同
表明硬链接文件和源文件是同一个文件,且链接编号显示这两个文件都有两个链接

注意:在同一物理介质的文件之间只能创建一个硬链接。不能在不同挂载点下的文件之间创建硬链接,在这种情况下,必须使用软连接。

方法二:使用ln(取link之意),ln源文件 硬链接文件
默认情况下,ln命令创建的是硬链接文件

[root@hadoop tmp]# ln test_file2 test_file22
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 2 root root  7 8月  10 18:56 test_file2
16811794 -rw-r--r--. 2 root root  7 8月  10 18:56 test_file22
16811795 -rw-r--r--. 1 root root  7 8月  10 18:48 test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1

2、创建软链接的方式
方法一:cp -s 源文件 软链接文件

[root@hadoop tmp]# cp -s test_file1 test_file5
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 1 root root  7 8月  10 18:56 test_file2
16811795 -rw-r--r--. 1 root root  7 8月  10 18:48 test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1

-s参数创建衣蛾符号链接(又称软链接)
注意:
1> 软链接文件和源文件的索引节点编号不同
2> 软链接文件和源文件的最后修改时间不同
3> 软链接文件和源文件的文件大小不同

表明软链接文件和源文件是不同的文件,链接文件只需要存储有关源文件的信息,而不是实际数据。文件名部分显示了这两个文件之间的关系。
所有软链接的处理方式是:底层文件不存在,那么链接指向的内容也就不存在了。

方法二:ln -s 源文件 软链接文件,使用-s 参数创建软链接文件

[root@hadoop tmp]# ln -s test_file3 test_file33
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 2 root root  7 8月  10 18:56 test_file2
16811794 -rw-r--r--. 2 root root  7 8月  10 18:56 test_file22
16811795 -rw-r--r--. 1 root root  7 8月  10 18:48 test_file3
16811797 lrwxrwxrwx. 1 root root 10 8月  11 00:02 test_file33 -> test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1

硬链接文件和软链接文件的区别:
1、由上面的示例可以看出,软链接文件的大小与源文件不同,软链接文件存储的是 软链接到源文件指向关系,所以很好理解他们的大小不同,可以理解为指针。
2、硬链接文件的索引节点编号,大小,最后修改时间完全相同,可以理解为 cp -p 了源文件,拷贝了一份源文件,且保留的源文件的属性

[注意,真正的cp -p 目标文件和源文件的节点编号不同,可以这么理解,cp -p 详见:https://blog.csdn.net/yaoyelinger0912/article/details/99098507]
3、修改文件的不同,修改源文件,硬链接文件的最后修改时间同步修改,软链接文件的最后修改时间不变,这里也充分说明了 软链接存储的只是源文件的位置和信息,而硬链接和源文件是同一个文件。

[root@hadoop tmp]# cat test_file2
12
123
[root@hadoop tmp]# cat test_file3
12
123
[root@hadoop tmp]# echo "追加2" >> test_file2 
[root@hadoop tmp]# echo "追加3" >> test_file3
[root@hadoop tmp]# cat test_file2
12
123
追加2
[root@hadoop tmp]# cat test_file3
12
123
追加3
[root@hadoop tmp]# cat test_file22
12
123
追加2
[root@hadoop tmp]# cat test_file33
12
123
追加3
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 2 root root 15 8月  11 00:16 test_file2
16811794 -rw-r--r--. 2 root root 15 8月  11 00:16 test_file22
16811795 -rw-r--r--. 1 root root 15 8月  11 00:16 test_file3
16811797 lrwxrwxrwx. 1 root root 10 8月  11 00:02 test_file33 -> test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1

注意:修改链接文件,源文件同步更改,适用于软连接和硬链接。

[root@hadoop tmp]# echo "追加22" >> test_file22
[root@hadoop tmp]# echo "追加33" >> test_file33
[root@hadoop tmp]# cat test_file2
12
123
追加2
追加22
[root@hadoop tmp]# cat test_file22
12
123
追加2
追加22
[root@hadoop tmp]# cat test_file3
12
123
追加3
追加33
[root@hadoop tmp]# cat test_file33
12
123
追加3
追加33
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 2 root root 24 8月  11 00:21 test_file2
16811794 -rw-r--r--. 2 root root 24 8月  11 00:21 test_file22
16811795 -rw-r--r--. 1 root root 24 8月  11 00:21 test_file3
16811797 lrwxrwxrwx. 1 root root 10 8月  11 00:02 test_file33 -> test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1

修改硬链接文件或者源文件,修改的时同一个文件,索引无论修改源文件还是硬链接文件,最后修改时间和文件大小都同步更改很好理解。
修改软连接文件,真正修改是源文件。软连接文件存储的是源文件的位置和信息,所以软连接文件的大小和最后修改时间不变。源文件的大小和最后修改时间修改。

4、删除文件的不同:删除硬链接源文件,硬链接文件依然可用。删除软连接文件的源文件,软连接不可用
在linux系统中,一个索引节点编号可以对应多个文件名,删除了硬链接文件的源文件,只是删除了索引节点到硬链接文件的源文件的映射关系,索引节点到硬链接文件的映射依然存在,索引硬链接文件依然可用。

删除了软连接文件的源文件,由于软连接存储的是源文件的位置和信息,查看软连接文件,源文件已被删除,所以软连接也不可用。

[root@hadoop tmp]# rm test_file2 -f
[root@hadoop tmp]# rm test_file3 -f
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 24 8月  11 00:21 test_file22
16811797 lrwxrwxrwx. 1 root root 10 8月  11 00:02 test_file33 -> test_file3
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月  10 23:53 test_file5 -> test_file1
[root@hadoop tmp]# cat test_file22
12
123
追加2
追加22
[root@hadoop tmp]# cat test_file33
cat: test_file33: 没有那个文件或目录
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值