什么是软链接、硬链接

什么是软链接、硬链接

一.软链接

①软链接的概念

类同与windos的快捷方式,给文件创建一个快速的访问路径,它依赖于原文件,与普通文件没什么不同,inode 都指向同一个文件在硬盘中的区块。当原文件出现问题后,该链接不可用。

(1)可以应用于目录

(2)可以跨文件系统

(3)不会增加被链接文件的链接次数

(4)大小为指定的绝对路径所包含的字符总数

(5)有自己的inode号

(6)权限无关紧要

②如何实现软链接:

命令 ln -s

下面我创建了一个file1的文件,并指定它软链接到 /mnt路径下的file2 。
查看file2,可以发现内容与file1的内容一致。
当删除file1时,file2失效。

[root@localhost /]# touch /opt/file1
[root@localhost /]# touch /opt/file1  >123abc
[root@localhost /]# rm -rf  /opt/file1
[root@localhost /]# touch /opt/file1
[root@localhost /]# echo 123abv > /opt/file1 
[root@localhost /]# ln -s /opt/file1  /mnt/file2
[root@localhost /]# cat /mnt/file2 
123abv
[root@localhost /]# rm -rf /opt/file1 
[root@localhost /]# ll /opt/file1 /mnt/file2 
ls: 无法访问/opt/file1: 没有那个文件或目录
lrwxrwxrwx. 1 root root 10 11月 27 13:12 /mnt/file2 -> /opt/file1
[root@localhost /]# cat /mnt/file2 
cat: /mnt/file2: 没有那个文件或目录

二.硬链接

①硬链接的概念:
指通过索引节点来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户 就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和 其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

(1) 硬链接,以文件副本的形式存在。但不占用实际空间。

(2) 不允许给目录创建硬链接。

(3) 硬链接只有在同一个文件系统中才能创建。

(4) 删除其中一个硬链接文件并不影响其他有相同 inode 号的文件。

②如何实现硬链接:

命令 ln

下面我创建了一个file1的文件,并指定它硬链接到 /mnt路径下的file2 。
查看file2,可以发现内容与file1的内容一致, inode也一致 。
当删除file1时,file2正常使用。

[root@localhost /]# rm -rf /mnt/file2 
[root@localhost /]# touch /opt/file1
[root@localhost /]# echo 658bcd  > /opt/file1 
[root@localhost /]# ln  /opt/file1  /mnt/file2
[root@localhost /]# ll /opt/file1 /mnt/file2 
-rw-r--r--. 2 root root 7 11月 27 13:21 /mnt/file2
-rw-r--r--. 2 root root 7 11月 27 13:21 /opt/file1
[root@localhost /]# rm -rf /opt/file1 
[root@localhost /]# ll /opt/file1 /mnt/file2
ls: 无法访问/opt/file1: 没有那个文件或目录
-rw-r--r--. 1 root root 7 11月 27 13:21 /mnt/file2
[root@localhost /]# cat /mnt/file2 
658bcd

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

king config

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值