链接文件是Linux文件系统的一个优势,它可以保存一份物理文件副本和多个虚拟副本。这种虚拟的副本称为链接。链接是目录中指向文件真实位置的占位符。Linux系统中有两种链接:符号链接和硬链接。

1. ln -s

符号链接是实实在在的文件,它指向虚拟目录结构中某个地方的另一个文件。两个文件通过符号链接在一起,内容并不相同。要为一个文件创建一个符号链接,原始文件必须存在。ln -s命令用来创建符号链接。

# 命令形式:ls -s source_file_name link_filename

# 创建符号链接
$ ln -s test b

# b为链接文件,test为源文件,`->`符号表明该文件是一个链接文件,文件b的大小与test不一样。
$ ls -l
total 8
-rw-r--r--  1 ***  staff  0 11 13 13:52 a
lrwxr-xr-x  1 ***  staff  4 11 13 15:11 b -> test
-rw-r--r--  1 ***  staff  0 11 13 13:13 test

2. ln

硬连接会创建独立的虚拟文件,其中包含了原始文件的位置和信息,但它们从根本上是一个文件,引用硬连接文件就等同于引用了源文件。创建硬连接时源文件也必须存在,不过ln命令不需要加上-s参数。

# 命令形式:ls source_file_name link_filename

# 创建硬连接
$ ln test c

# c为硬链接文件
$ ls -l
total 8
-rw-r--r--  1 ***  staff  0 11 13 13:52 a
lrwxr-xr-x  1 ***  staff  4 11 13 15:11 b -> test
-rw-r--r--  2 ***  staff  0 11 13 13:13 c
-rw-r--r--  2 ***  staff  0 11 13 13:13 test

# c和test的inode编码是一样的,且它们的文件大小也是一样的。
$ ls -li
total 8
6558451 -rw-r--r--  1 ***  staff  0 11 13 13:52 a
6559485 lrwxr-xr-x  1 ***  staff  4 11 13 15:11 b -> test
6557711 -rw-r--r--  2 ***  staff  0 11 13 13:13 c
6557711 -rw-r--r--  2 ***  staff  0 11 13 13:13 test
912sy.com download resources are from the network, only for learning and reference use, the copyright belongs to the original author, do not use for commercial purposes, please remove yourself within 24 hours after downloading.
If the content published on this site inadvertently violates your rights and interests, please contact us and the site will be deleted within one business day.If you encounter any problems please contact customer service QQ:2385367137
912sy " Introduction to the ln command