【Linux基础】文件链接

综述

Linux中的链接的意思是创建一个文件,使它指向原有的文件。链接的方式有两种,一种称为硬链接,一种称为软连接(也叫符号链接)。两者有些差异,简单来说软链接是创建了一个新的文件,这个文件指向原来的文件;而硬链接跟源文件没有实质的差异,两者只是路径和名称不同而已。

链接使用ln命令来实现,它的说明如下:

用法:ln [选项]... [-T] 目标 链接名	(第一种格式)
 或:ln [选项]... 目标		(第二种格式)
 或:ln [选项]... 目标... 目录	(第三种格式)
 或:ln [选项]... -t 目录 目标...	(第四种格式)
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.

必选参数对长短选项同时适用。
      --backup[=CONTROL]	为每个已存在的目标文件创建备份文件
  -b				类似--backup,但不接受任何参数
  -d, -F, --directory		创建指向目录的硬链接(只适用于超级用户)
  -f, --force			强行删除任何已存在的目标文件
  -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		显示此帮助信息并退出
      --version		显示版本信息并退出

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

Using -s ignores -L and -P.  Otherwise, the last option specified controls
behavior when a TARGET is a symbolic link, defaulting to -P.

目标指的是需要被链接的源文件,链接名是新创建的用来链接的文件的名称,目录是需要创建链接文件的目录。软链接的创建需要增加参数-s,表示符号(symbolic)链接,硬链接的创建则不需要。

示例

首先创建一个源文件用来被链接,然后创建软硬链接并显示:

$ touch src_file
$ ln src_file hand_link
$ ln -s src_file soft_link
$ ls -ali
总用量 8
12072424 drwxr-xr-x 2 jw jw 4096 10月 17 11:18 .
11946004 drwxr-xr-x 6 jw jw 4096 10月 17 11:09 ..
12072425 -rw-r--r-- 2 jw jw    0 10月 17 11:18 hand_link
12072426 lrwxrwxrwx 1 jw jw    8 10月 17 11:18 soft_link -> src_file
12072425 -rw-r--r-- 2 jw jw    0 10月 17 11:18 src_file

这里有几点值得关注的:

首先是软连接在显示时有“->”这样的指向性表示,而硬链接是没有的。

再看显示的第一列,表示的是inode值,硬链接和源文件是一致的,而软连接却不同,这跟前面叫的软链接是新文件而硬链接跟源文件基本一致的说法对应起来了;这里还需要注意一点,硬链接跟源文件的inode一致,这也基本表示了硬链接和源文件必须是在同一个文件系统下的。

还有第二列的操作权限可以看到硬链接和源文件也是一致的,但是软连接却是全部用户都有读写执行权限,但是这并不表示所有用户都可以更改源文件,而只是可以操作软链接而已。

进一步说明软硬链接的差异,这里直接删除源文件,然后再读取软硬链接:

$ rm src_file 
$ ll
总用量 8
drwxr-xr-x 2 jw jw 4096 10月 17 11:51 .
drwxr-xr-x 6 jw jw 4096 10月 17 11:09 ..
-rw-r--r-- 1 jw jw    0 10月 17 11:18 hand_link
lrwxrwxrwx 1 jw jw    8 10月 17 11:18 soft_link -> src_file
$ cat soft_link 
cat: soft_link: 没有那个文件或目录
$ cat hard_link

这时候软链接已经无法访问,而硬链接还是可以访问的(因为是空的,所以没有打印,但是不会报错。此时如果看软链接显示的颜色,也可以看出已经发生了变化:

在Linux中目录也属于文件,所以理论上也是可以为其创建链接的,但是实际的测试中发现可以创建软链接但是无法创建硬链接,下面是一个例子:

$ mkdir src_dir
$ mkdir soft_dir
$ mkdir hard_dir
$ ls
hard_dir  soft_dir  src_dir
$ cd soft_dir/
$ ln -s ../src_dir/
$ ll
总用量 8
drwxr-xr-x 2 jw jw 4096 10月 17 12:48 .
drwxr-xr-x 5 jw jw 4096 10月 17 12:48 ..
lrwxrwxrwx 1 jw jw   11 10月 17 12:48 src_dir -> ../src_dir/
$ cd ..
$ cd hard_dir/
$ ln ../src_dir/
ln: ../src_dir/: 不允许将硬链接指向目录

可以看到创建目录的硬链接报错了。关于这一点,在《UNIX环境高级编程》中有说明:

从这里的说明来看超级用户可以创建目录的硬链接,但是需要文件系统本身的支持,不过目前来看ext4是不支持的。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值