linux 的硬链接与软连接

linux 里有硬链接和软连接两种概念。要明白这些概念首先要明白文件在linux 上其实有3个组成部分。

data 真正的数据存储区域

inode 一个用来唯一表示data的数据结构

filename 指向inode

硬链接

hard link是说增加一个filename point到inode。 这里的重点是inode不变,data不变。只是filename多了一个。因为inode 不变,而inode是跟文件系统一一对应的,所以硬链接不能跨文件系统。

软连接

soft link是说增加了一个新的文件,即一个新的data区域 一个新的inode, 一个新的filename。但是data 中存储的内容是指向源文件的一个指针。因为软连接多了一个inode, 所以软连接可以跨文件系统。

下面的步骤可以更详细的展示文件,硬链接 软连接的关系

首先创建一个文件large。 如下例子中

stat 显示了 Inode 为4298673113 

ls -l 显示了 large文件的大小为9.4G 引用数 为1 (-rw-r--r--. 1 这里的1是引用数)

[root@node1 test]# stat large 
  File: ‘large’
  Size: 10000000000	Blocks: 19531256   IO Block: 4096   regular file
Device: fd02h/64770d	Inode: 4298673113  Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-09-24 15:55:42.703757216 +0800
Modify: 2015-09-24 15:55:51.054222277 +0800
Change: 2015-09-24 16:14:12.166408739 +0800
 Birth: -
[root@node1 test]# 
[root@node1 test]# ls -lh large 
-rw-r--r--. 1 root root 9.4G Sep 24 15:55 large

其次我们创建一个硬链接如下:

stat 显示了 Inode 为4298673113 ,两个文件都是。

ls -l 显示了 两个文件的大小为9.4G 引用数 为2 

这是因为硬链接只是为文件多建立了个名字。即引用多了一个。而inode不变。文件的大小也不变。

[root@node1 test]# stat large
  File: ‘large’
  Size: 10000000000	Blocks: 19531256   IO Block: 4096   regular file
Device: fd02h/64770d	Inode: 4298673113  Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-09-24 15:55:42.703757216 +0800
Modify: 2015-09-24 15:55:51.054222277 +0800
Change: 2015-09-24 16:17:47.855250627 +0800
 Birth: -
[root@node1 test]# ls -lh large
-rw-r--r--. 2 root root 9.4G Sep 24 15:55 large
[root@node1 test]# 
[root@node1 test]# stat large.hard 
  File: ‘large.hard’
  Size: 10000000000	Blocks: 19531256   IO Block: 4096   regular file
Device: fd02h/64770d	Inode: 4298673113  Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-09-24 15:55:42.703757216 +0800
Modify: 2015-09-24 15:55:51.054222277 +0800
Change: 2015-09-24 16:17:47.855250627 +0800
 Birth: -
[root@node1 test]# 
[root@node1 test]# ls -lh large.hard 
-rw-r--r--. 2 root root 9.4G Sep 24 15:55 large.hard
[root@node1 test]# 

我们在创建一个软连接文件

stat 显示inode变了

ls -lh 显示引用为1 而大小也变了。

这是因为软连接是一个新的文件,其filename是新的,inode是新的,data区域也是新的。data区域的内容是指向源文件的。所以引用为1 inode 不一样而且大小也不一样

[root@node1 test]# stat large.soft
  File: ‘large.soft’ -> ‘large’
  Size: 5         	Blocks: 0          IO Block: 4096   symbolic link
Device: fd02h/64770d	Inode: 4298672263  Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-09-24 16:21:35.503190700 +0800
Modify: 2015-09-24 16:21:35.503190700 +0800
Change: 2015-09-24 16:21:35.503190700 +0800
 Birth: -
[root@node1 test]# ls -lh large.soft 
lrwxrwxrwx. 1 root root 5 Sep 24 16:21 large.soft -> large

  

更进一步去理解硬链接可以用如下的例子:

我们有如下3个文件

[root@node1 test]# ls -l *
-rw-r--r--. 1 root root 10000000000 Sep 24 16:23 big
-rw-r--r--. 2 root root 10000000000 Sep 24 15:55 large
-rw-r--r--. 2 root root 10000000000 Sep 24 15:55 large.hard

这几个文件大小一样。large和large.hard互为硬链接。

我们再看man rm 输出的一句话

Remove (unlink) the FILE(s)

这说明rm操作其实是给文件去掉一个link (也就是硬链接)但如果文件去掉一个硬链接之后没有更多的引用了,操作系统会回收文件所占用的空间。

因此,我们可以断定,rm big 所需要的时间要远大于rm large 。 因为big只有一个硬链接,我们在rm后操作系统发现没有filename引用该文件,操作系统需要去回收空间。 但large不一样。 操作系统在unlink large后发现还有一个硬链接引用,所以不用去回收空间。 测试如下:

[root@node1 test]# time rm -f big

real	0m1.753s
user	0m0.000s
sys	0m1.750s
[root@node1 test]# time rm -f large

real	0m0.001s
user	0m0.000s
sys	0m0.001s

  

原文地址:https://www.cnblogs.com/kramer/p/4835731.html