【Linux】软链接 VS 硬链接

软链接(soft link)和硬链接(hard link)在Linux的使用中是一个极其重要的概念,创建链接的命令是ln.
ln的man page中对命令的解释是make links between files

创建链接的方式也比较简单

[jiangjian@localhost shell]$ ln -s a.txt a-soft-link.txt
[jiangjian@localhost shell]$ ln a.txt a-hard-link.txt # 注意其中没有-s参数
[jiangjian@localhost shell]$ ll
total 8
-rw-rw-r--. 2 jiangjian jiangjian 21 Apr  7 02:27 a-hard-link.txt
lrwxrwxrwx. 1 jiangjian jiangjian  5 Apr  7 02:28 a-soft-link.txt -> a.txt
-rw-rw-r--. 2 jiangjian jiangjian 21 Apr  7 02:27 a.txt
[jiangjian@localhost shell]$ cat a-hard-link.txt 
This is shell script
[jiangjian@localhost shell]$ cat a-soft-link.txt 
This is shell script
[jiangjian@localhost shell]$ cat a.txt 
This is shell script
[jiangjian@localhost shell]$ 

那么两者有什么区别呢?

软链接:

  • 能够跨文件系统创建软链接;
  • 可以创建目录之间的链接;
  • 软链接的文件独立与源文件,有独立的inode和权限控制;
  • 软链接只有源文件的路径信息,并不包含内容;

硬链接:

  • 不能跨文件系统;
  • 不能链接目录;
  • 和源文件拥有相同的inode和权限
  • 硬链接权限修改会同样导致源文件权限修改;
  • 拥有源文件的内容,即使源文件删除,硬链接仍可以查看内容;
[jiangjian@localhost shell]$ ll -i
total 8
103981504 -rw-rw-r--. 2 jiangjian jiangjian 21 Apr  7 02:27 a-hard-link.txt
103981509 lrwxrwxrwx. 1 jiangjian jiangjian  5 Apr  7 02:28 a-soft-link.txt -> a.txt
103981504 -rw-rw-r--. 2 jiangjian jiangjian 21 Apr  7 02:27 a.txt
[jiangjian@localhost shell]$ rm a.txt 
[jiangjian@localhost shell]$ cat a-soft-link.txt 
cat: a-soft-link.txt: No such file or directory
[jiangjian@localhost shell]$ cat a-hard-link.txt 
This is shell script
[jiangjian@localhost shell]$ 

通过上面的输出可以看到硬链接和源文件在inode和权限控制上都一样,而软链接的文件拥有独立的inode和权限列表.

当我们删除源文件后,会导致软链接指向的文件不存在,而硬链接从理解上可以理解为拷贝了源文件,所以仍可以输出内容.

那么从底层考虑如何实现这两种方式的呢?

软链接: 其实存储的是对应源文件的路径,如果源文件被删除,则对应的路径就会无效,故会导致No such file or directory错误;

硬链接:硬链接和源文件共享同一个inode,其实两者表示的就是同一个文件,只不过此时inode里面的link数量会为2,用来记录文件被应用的次数,默认新建文件值为1, 创建一个硬链接,就会累加一次,

[jiangjian@localhost shell]$ stat a.txt 
  File: ‘a.txt’
  Size: 21        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 103981504   Links: 2
Access: (0664/-rw-rw-r--)  Uid: ( 1000/jiangjian)   Gid: ( 1000/jiangjian)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2019-04-07 02:35:11.625140133 -0400
Modify: 2019-04-07 02:27:48.546122615 -0400
Change: 2019-04-07 02:35:02.160075674 -0400
 Birth: -

当源文件或者硬链接问价删除后,操作系统会检查当前的link数是否为0,如果为0,则说明没有文件指向当前inode了,inode可以回收,上面的源文件删除后,我们可以了解到link应该为1了.

[jiangjian@localhost shell]$ stat a-hard-link.txt 
  File: ‘a-hard-link.txt’
  Size: 21        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 103981504   Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/jiangjian)   Gid: ( 1000/jiangjian)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2019-04-07 02:35:11.625140133 -0400
Modify: 2019-04-07 02:27:48.546122615 -0400
Change: 2019-04-07 02:35:02.160075674 -0400
 Birth: -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值