Linux-链接文件

我们先看看Linux 的manual 手册中对 ln 命令的说明:
#man ln
NAME
ln - make links between files

SYNOPSIS
ln [OPTION]… [-T] TARGET LINK_NAME (1st form)
ln [OPTION]… TARGET (2nd form)
ln [OPTION]… TARGET… DIRECTORY (3rd form)
ln [OPTION]… -t DIRECTORY TARGET… (4th form)

DESCRIPTION
In the 1st form, create a link to TARGET with the name LINK_NAME. In
the 2nd form, create a link to TARGET in the current directory. In the
3rd and 4th forms, create links to each TARGET in DIRECTORY. Create
hard links by default, symbolic links with –symbolic. By default,
each destination (name of new link) should not already exist. When
creating hard links, each TARGET must exist. Symbolic links can hold
arbitrary text; if later resolved, a relative link is interpreted in
relation to its parent directory.
主要参数如下:
Mandatory arguments to long options are mandatory for short options too.
–backup[=CONTROL]
make a backup of each existing destination file
-b like –backup but does not accept an argument
-d, -F, –directory
allow the superuser to attempt to hard link directories (note:
will probably fail due to system restrictions, even for the
superuser)
-f, –force
remove existing destination files
-i, –interactive
prompt whether to remove destinations
-L, –logical
dereference TARGETs that are symbolic links
-n, –no-dereference
treat LINK_NAME as a normal file if it is a symbolic link to a
directory
-P, –physical
make hard links directly to symbolic links
-r, –relative
create symbolic links relative to link location
-s, –symbolic
make symbolic links instead of hard links
-S, –suffix=SUFFIX
override the usual backup suffix
-t, –target-directory=DIRECTORY
specify the DIRECTORY in which to create the links
-T, –no-target-directory
treat LINK_NAME as a normal file always
-v, –verbose
print name of each linked file
–help
display this help and exit
–version
output version information and exit
可以看到ln命令默认建立硬链接(hard link),使用-s参数,可以建立软链接(符号链接symbolic link)。
第一种形式:如果ln命令后面给出两个文件名,那么第一个文件是被链接的文件,第二个文件是一个链接文件。比如说,ln TARGET LINK_NAME ,就是建立一个链接文件名叫LINK_NAME,它链接了TARGET这个文件。
我们在当前目录下建立一个文本tmp.txt,然后查看它的属性:
-rw-rw-rw- 1 test testgroup 94 1月 27 11:40 tmp.txt
可以看到它的被链接数是1。
然后执行ln tmp.txt linktmp
再次查看文件属性,得到:
-rw-rw-rw- 2 test testgroup 94 1月 27 11:40 linktmp
-rw-rw-rw- 2 test testgroup 94 1月 27 11:40 tmp.txt
看到被链接数变成了2。
接下来我们建立一个符号链接,
ln -s tmp.txt softlink
然后查看文件属性:
-rw-rw-rw- 2 test testgroup 94 1月 27 11:40 linktmp
lrwxrwxrwx 1 test testgroup 7 1月 27 16:18 softlink -> tmp.txt
-rw-rw-rw- 2 test testgroup 94 1月 27 11:40 tmp.txt
注意,这时softlink是一个独立的文件,文件属性不是’-‘而是’l’,表示它是一个链接文件,而且它的被链接数是1,文件属性也发生了变化,增加了’x’可执行的属性。
那如果我们删除了tmp.txt,会怎么样呢?
再次查看文件属性:
-rw-rw-rw- 1 test testgroup 94 1月 27 11:40 linktmp
可以发现硬链接文件的链接数变成了1。
这时如果要查看linktmp的内容,还是可以顺利操作。
但是查看softlink的内容就会报错:
cat: softlink: 没有那个文件或目录

总结一下,修改硬链接或者软链接的内容,都会同时修改所有的被链接的文件。但是删除源文件之后,软链接文件会失效,硬链接文件仍然正常使用。
可以简单理解成,硬链接跟复制的功能很像,但是硬链接占用存储容量很小,这是跟复制最大的区别,因为它还是指向原来的文件的。
而软链接就是一个快捷方式。只不过,当你修改这个快捷方式的数据时,修改的其实是原始数据。
所以对链接文件进行操作时要主要,一旦删除了数据,原始数据也会被删除。
我们通过stat命令查看文件的详细信息:
文件:’softlink’ -> ‘tmp.txt’
大小:7 块:0 IO 块:4096 符号链接
设备:803h/2051d Inode:541505 硬链接:1

文件:’hardlink’
大小:125 块:8 IO 块:4096 普通文件
设备:803h/2051d Inode:546202 硬链接:2

文件:’tmp.txt’
大小:125 块:8 IO 块:4096 普通文件
设备:803h/2051d Inode:546202 硬链接:2

可以发现,硬链接和原始文件其实是使用了同一个inode,但是软链接独自占用一个inode。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值