hard link 和 symblic link 之间的区别 +证明hard link 不消耗额外的硬盘空间

本文详细阐述了Unix文件系统的硬链接与软链接的工作原理,通过实例演示如何创建这两种链接,以及它们对磁盘空间的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Unix文件包含两个部分:数据部分和文件名部分

Unix files consist of two parts: the data part and the filename part.

文件名部分包含了一个名字和相关连的inode id

The filename part carries a name and an associated inode number.

                         .--------------> ! permbits, etc ! addresses !
                        /                 +---------inode-------------+
        ! filename ! inode # !
        +--------------------+
数据部分包括inode部分,inode部分则装载有实际内容的地址,文件访问权限等信息。

The data part is associated with something called an 'inode'. The inode carries the map of where the data is, the file permissions, etc. for the data.

                               .---------------> ! data ! ! data ! etc
                              /                  +------+ !------+
        ! permbits, etc ! data addresses !
        +------------inode---------------+

多个文件名可以引用同一个inode id,这就是所谓的硬连接。

More than one filename can reference the same inode number; these files are said to be 'hard linked' together.

        ! filename ! inode # !
        +--------------------+
                        \
                         >--------------> ! permbits, etc ! addresses !
                        /                 +---------inode-------------+
        ! othername ! inode # !
        +---------------------+

另外,有一个特殊的文件,其数据部分包含的是另外一个文件的路径。操作系统读取它的数据,并作为路径进行重定向、打开、读写等操作而不是访问这个特殊文件的内容。该文件又称为软连接。

On the other hand, there's a special file type whose data part carries a path to another file. Since it is a special file, the OS recognizes the data as a path, and redirects opens, reads, and writes so that, instead of accessing the data within the special file, they access the data in the file named by the data in the special file. This special file is called a 'soft link' or a 'symbolic link' .

        ! filename ! inode # !
        +--------------------+
                        \
                         .-------> ! permbits, etc ! addresses !
                                   +---------inode-------------+
                                                      /
                                                     /
                                                    /
    .----------------------------------------------'
   ( 
    '-->  !"/path/to/some/other/file"! 
          +---------data-------------+
                  /                      }
    .~ ~ ~ ~ ~ ~ ~                       }-- (redirected at open() time)
   (                                     }
    '~~> ! filename ! inode # !
         +--------------------+
                         \
                          '------------> ! permbits, etc ! addresses !
                                         +---------inode-------------+
                                                            /
                                                           /
     .----------------------------------------------------'
    (
     '->  ! data !  ! data ! etc.
          +------+  +------+ 


ln in action

1、进入一个空文件夹:

jung@jungsagacity:/software/install$ ls -a
.  ..

2、查看文件使用空间大小:
jung@jungsagacity:/software/install$ du -cb .
4096    .
4096    总用量

(此处就不用查 .. 文件所占用的空间了,因为它是当前目录的父目录的硬连接,就像. 文件是当前目录的硬连接一样)


3、新建一个文件:

jung@jungsagacity:/software/install$ echo "123" >a
jung@jungsagacity:/software/install$ cat a
123
jung@jungsagacity:/software/install$ ls -a
.  ..  a

4、查看文件a的大小和该目录总的使用空间大小:
jung@jungsagacity:/software/install$ du -cb a
4	a
4	总用量
jung@jungsagacity:/software/install$ du -cb 
4100	.
4100	总用量
jung@jungsagacity:/software/install$ 

(a文件的内容为“123”,正好占四个字节,加上之前当前目录所站的字节数,总共4100个字节)


5、建立硬链接:

jung@jungsagacity:/software/install$ ln a a-hard
jung@jungsagacity:/software/install$ ls -a
.  ..  a  a-hard
jung@jungsagacity:/software/install$ ls -il
总用量 8
1058144 -rw-rw-r-- 2 jung jung 4  5月 31 16:43 a
1058144 -rw-rw-r-- 2 jung jung 4  5月 31 16:43 a-hard

(建立硬连接之后,可以发现源文件的inode id和硬连接文件的inode id 是一样的,所占空间大小、读写权限、所属用户和用户组等都是一样的)

(第三个参数 “2” 表示的是连接数为2)


6、查看硬连接文件是否占用了额外的磁盘空间

jung@jungsagacity:/software/install$ du -cb 
4100	.
4100	总用量

(当前目录的使用空间依然没有变化)


7、建立软连接,查看是否占用了额外的硬盘空间:

jung@jungsagacity:/software/install$ ln -s a a-soft
jung@jungsagacity:/software/install$ ls -a
.  ..  a  a-hard  a-soft
jung@jungsagacity:/software/install$ ls -il
总用量 8
1058144 -rw-rw-r-- 2 jung jung 4  5月 31 16:43 a
1058144 -rw-rw-r-- 2 jung jung 4  5月 31 16:43 a-hard
1058280 lrwxrwxrwx 1 jung jung 1  5月 31 16:54 a-soft -> a

(建立软连接之后,有一个标识:a-soft -> a,很形象啊 !另外可以发现,a-soft 有自己独立的inode id,因此a-soft已经是一个独立的文件了。)


jung@jungsagacity:/software/install$ du -cb a-soft
1	a-soft
1	总用量
jung@jungsagacity:/software/install$ du -cb
4101	.
4101	总用量

(a-soft占用了一个字节的空间,这一点有点迷惑。如果说它存储的是源文件的路径的话,怎么其中的内容就仅仅只有一个自己呢?希望有看官能帮忙解释下!)


总结:

之所以些这边日志,是因为lvdou同学再看《鸟哥的linux私房菜》的时候,冒出一个问题:如果cp和hard link类似的话,为什么hard link不会耗费额外的存储空间呢?

起初,我也认定鸟哥说的这个道理,毕竟“链接”如果还需要耗费和源文件一样大小的存储空间的话,就没有必要称之为“链接”了。于是我  ls -il 命令查询之后发现,如果增加了一个硬链接的话,total 总量会增加和源文件一样大小的数量。如果增加软连接的话,则增加软连接文件大小的空间。这一点让我看蒙了,于是就开始了上面的这段证明了!


PS : 

The listing of a directory's contents is preceded by a labeled total number of blocks used in the file system by the files which are listed as  the directory's contents (which may or may not include . and .. and other   files which start with a dot, depending on other options).

ls -il 之后的total  总量统计的是该文件下所有文件总共占有的块数(我的系统是ubuntu 12.04,每一块大小为1024B),对于硬连接也会将其实际所指向的内容包括在内。对于软链接,则只会计算这个软链接文件本身所占的空间大小。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值