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

链接是目录中的占位符,指向文件的真实位置(可以理解为指针)。
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: 没有那个文件或目录

1.10- 链接文件 及 硬链接和软连接的区别相关推荐

  1. linux链接文件:硬链接和软连接

     linux系统中链接文件分为硬链接和软链接(软链接也叫符号链接). 硬链接和软链接都是指向文件的一种方式,但两者有不同的地方,主要有以下不同: 硬链接:伪备份   软连接:快捷方式    1.硬 ...

  2. linux 复制 链接文件,Linux硬链接、软连接和复制的区别

    1. 硬连接 命令:ln file1 file2 限制:不能跨分区:文件夹无效. 作用:实现对file1的一个硬连接.不同于拷贝(复制). 效果:修改file1,file2会变:修改file2,fil ...

  3. linux目录硬链接,linux查看硬链接对应的所有文件

    在linux中,链接文件分为硬链接和软链接文件两种,其中硬链接通过ln source_file dist_file建立,软链接通过ln -s source_file dist_file建立. 软硬链接 ...

  4. linux 符号连接文件,Linux 硬链接和软链接(符号链接)

    什么是目录 Linux 文件系统是树状结构的.根目录下存在一系列子目录.目录里边有文件或者子目录. 但问题在于: 目录是什么? 文件又是什么? 文件是:数据 + 属性(比如名字.创建时间.所有者之类) ...

  5. 特殊权限 set_uid、set_gid、stick_bit,软链接文件,硬链接文件

            特殊权限 set_uid set_uid: 这里的s 就是set_uid 权限 linux 下的passwd文件是允许普通用户修改自己的密码的 (/etc/shadow :密码文件) ...

  6. Linux入门学习(四)—— 什么链接文件?软链接文件和硬链接文件有什么区别以及特点?

    引言:上期回顾(想要学习的童鞋可以点击看看) 前几章我们所说的命令都是针对文件和文件夹,和一些常用操作.今天我们来学习特殊一点的文件"链接文件",这一章节会告诉大家什么是链接文件, ...

  7. 符号链接文件和硬链接文件

    □□实验6-11.针对符号连接文件和硬连接文件. 测试以下函数,分析其执行过程并给出结论. link(),unlink(),symlink(),readlink(),stat(),lstat() 1. ...

  8. Linux之 如何查看文件是`硬链接`还是`软链接`

    Linux之 如何查看文件是硬链接还是软链接 可以用 ll 文件名 查看文件属性 以l开头的是软链接 , 相当于快捷方式 以-开头的是硬链接 实验 实验 1 创建一个文件名为"hello&q ...

  9. mac 建立软链接_linux/mac系统的软链接文件与硬链接文件

    1.硬连接只能使用在文件上,不可以使用在文件夹上.至于文件前面的硬链接数字的含义如下: 如图标注区,为硬连接的数量,文件前的数字1表示没有硬链接.文件夹前面的数字至少是2,含义是这个文件夹是空文件夹, ...

最新文章

  1. 项目工作展望(风来)
  2. linux csr蓝牙驱动,csr4.0蓝牙适配器驱动下载
  3. tf.stack()和tf.unstack()的用法
  4. boost::python模块实现使用任意 Python 序列创建 ndarray 的示例
  5. 老齐python-基础3(列表)
  6. EMNLP'21 | 多语言和跨语言对话推荐
  7. java程序设计精编教程第3版电子版课后答案_Java程序设计精编教程-(第3版)-微课版...
  8. RDPWrap,win10家庭版最新配置文件支持termsrv10.0.18362.836
  9. 新型的铁塔基站“能源管家”
  10. 魅族 android 文件传输,魅族文件管理app提取下载
  11. 计算机专业学微机原理与接口技术,信息技术学院计算机科学与技术专业《微机原理与接口技术.doc...
  12. lookup无序查找_Excel LOOKUP不排序怎么快速找到数据_lookup函数讲解
  13. 读书百客:《十一月四日风雨大作》评赏
  14. 给想立志入行网络或已经初入行的朋友的建议
  15. 服务器未能保存文件夹,Exchange服务器提示 Event ID 50 Ntfs (Ntfs) {延迟写入失败} Windows 无法保存文件...
  16. 【纯前端】原生js实现照片水印
  17. 两个向量的夹角解法--VC
  18. 查看linux系统IPV6地址
  19. 【CCF-CSP201712-4】行车路线
  20. 《妥协的完美主义—优秀产品经理的实践指南(卷一)》一1.3 UCD工作方法

热门文章

  1. 微星GS60 UHD 10.12.4 黑苹果教程
  2. 科学的整理计算机中的文件,如何快速整理电脑中的文件?
  3. 吴恩达:这些 AI 大事件让我无法忘怀...
  4. 这个会自己行走的机器人行李箱,根本就是辆小型“特斯拉无人汽车” | 钛空舱
  5. nginx 手机版页面判断_Nginx配置如何区分PC或手机访问不同域名
  6. php将字符串转json,php如何实现json转字符串
  7. android下载图片 服务器,Android网络客户端从服务器电脑上下载图片
  8. 大家应该知道的社保常识 看看吧会有用的
  9. 【FPGA教程案例54】深度学习案例1——基于FPGA的CNN卷积神经网络之理论分析和FPGA模块划分
  10. Linux数据库12154错误,ORA-12154 和 TNS-03505 监听错误的解决方法