简单理解 Linux 硬链接与软链接

最近在学Linux ,在链接上有了认识,写出来学习一下

先探究一下概念

硬链接

Hard links are the original Unix way of creating links, compared to symbolic links,which are more modern. By default,every file has a single hard link that gives the file its name. When we create a hard link, we create an additional directory entry for a file. Hard links have two important limitations:
硬链接和符号链接比起来,硬链接是最初 Unix 创建链接的方式,而符号链接更加现代。在 默认情况下,每个文件有一个硬链接,这个硬链接给文件起名字。当我们创建一个硬链接以后,就为文件创建了一个额外的目录条目。硬链接有两个重要局限性:
1. A hard link cannot reference a file outside its own file system. This means a link may not reference a file that is not on the same disk partition as the link itself.
2. A hard link may not reference a directory.

  1. 一个硬链接不能关联它所在文件系统之外的文件。这是说一个链接不能关联与链接本身 不在同一个磁盘分区上的文件。
  2. 一个硬链接不能关联一个目录。
    A hard link is indistinguishable from the file itself. Unlike a symbolic link, when you list a directory containing a hard link you will see no special indication of the link. When a hard link is deleted, the link is removed but the contents of the file itself continue to exist (that is, its space is not deallocated) until all links to the file are deleted. It is important to be aware of hard links because you might encounter them from time to time, but modern practice prefers symbolic links, which we will cover next.
一个硬链接和文件本身没有什么区别。不像符号链接,当你列出一个包含硬链接的目录内容时,你会看到没有特殊的链接指示说明。当一个硬链接被删除时,这个链接被删除,但是文件本身的内容仍然存在(这是说,它所占的磁盘空间不会被重新分配),直到所有关联这个文件的链接都删除掉。知道硬链接很重要,因为你可能有时会遇到它们,但现在实际中更喜欢使用符号链接,下一步我们会讨论符号链接。**

符号链接

Symbolic links were created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains a text pointer to the referenced file or directory. In this regard, they operate in much the same way as a Windows shortcut though of course, they predate the Windows feature by many years ;-)
创建符号链接是为了克服硬链接的局限性。符号链接生效,是通过创建一个特殊类型的文 件,这个文件包含一个关联文件或目录的文本指针。在这一方面,它们和 Windows 的快捷方 式差不多,当然,符号链接早于 Windows 的快捷方式很多年;-)
A file pointed to by a symbolic link, and the symbolic link it self are largely indistinguishable from one another.For example,if you write somes omething to the symbolic link,the referenced file is also written to. However when you delete a symbolic link, only the link is deleted, not the file itself. If the file is deleted before the symbolic link, the link will continue to exist, but will point to nothing. In this case, the link is said to be broken. In many implementations, the ls command will display broken links in a distinguishing color, such as red, to reveal their presence.**
一个符号链接指向一个文件,而且这个符号链接本身与其它的符号链接几乎没有区别。例如,如果你往一个符号链接里面写入东西,那么相关联的文件也被写入。然而,当你删除一个符号链接时,只有这个链接被删除,而不是文件自身。如果先于符号链接删除文件,这个链接 仍然存在,但是不指向任何东西。在这种情况下,这个链接被称为坏链接。在许多实现中,ls 命令会以不同的颜色展示坏链接,比如说红色,来显示它们的存在。


硬链接与软链接的联系与区别

我们知道文件都有文件名与数据,这在 Linux 上被分成两个部分:用户数据 (user data) 与元数据 (metadata)。用户数据,即文件数据块 (data block),数据块是记录文件真实内容的地方;而元数据则是文件的附加属性,如文件大小、创建时间、所有者等信息。在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 号寻找正确的文件数据块。
若一个 inode 号对应多个文件名,则称这些文件为硬链接。换言之,硬链接就是同一个文件使用了多个别名

软链接与硬链接不同,若文件用户数据块中存放的内容是另一文件的路径名的指向,则该文件就是软连接。软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的 inode 号以及用户数据块

下面可以通过两个简单实验加深理解(转)

[oracle@Linux] touch f1 #创建一个测试文件f1
[oracle@Linux] ln f1 f2 #创建f1的一个硬连接文件f2
[oracle@Linux] ln -s f1 f3 #创建f1的一个符号连接文件f3
[oracle@Linux] ls -li # -i参数显示文件的inode节点信息
total 0
9797648 -rw-r–r– 2 oracle oinstall 0 Apr 21 08:11 f1
9797648 -rw-r–r– 2 oracle oinstall 0 Apr 21 08:11 f2
9797649 lrwxrwxrwx 1 oracle oinstall 2 Apr 21 08:11 f3 -> f1

从上面的结果中可以看出,硬连接文件f2与原文件f1的inode节点相同,均为9797648,然而符号连接文件的inode节点不同。

[oracle@Linux] echo “I am f1 file” >>f1
[oracle@Linux] cat f1
I am f1 file
[oracle@Linux] cat f2
I am f1 file
[oracle@Linux] cat f3
I am f1 file
[oracle@Linux] rm -f f1
[oracle@Linux] cat f2
I am f1 file
[oracle@Linux] cat f3
cat: f3: No such file or directory

通过上面的测试可以看出:当删除原始文件f1后,硬连接f2不受影响,但是符号连接f1文件无效

3.总结
依此您可以做一些相关的测试,可以得到以下全部结论:
1).删除符号连接f3,对f1,f2无影响;
2).删除硬连接f2,对f1,f3也无影响;
3).删除原文件f1,对硬连接f2没有影响,导致符号连接f3失效;
4).同时删除原文件f1,硬连接f2,整个文件会真正的被删除。

深入了解可以参考http://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值