Links in Linux

Summary

The link in Linux means to create a file and make it point to the original file. There are two ways of linking, one is called hard link, and the other is called soft link (also called symbolic link). There are some differences between the two. In simple terms, a soft link creates a new file that points to the original file; while a hard link has no substantial difference from the source file, the two are just different in path and name.

The link is implemented using the ln command, and its description is as follows:

用法: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.

The target refers to the source file that needs to be linked, the link name is the name of the newly created file to be linked, and the directory is the directory where the linked file needs to be created. The creation of soft links requires the addition of the parameter -s, which represents a symbolic link, while the creation of hard links does not.

 

Example

First create a source file to be linked, then create soft and hard links and display:

$ 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

Here are a few points worth noting:

The first is that soft links have a directional indication such as "->" when displayed, while hard links do not.

Looking at the first column of the display, it shows the inode value. The hard link is the same as the source file, but the soft link is different. This corresponds to the statement that the soft link is a new file and the hard link is basically the same as the source file. It's up; here is another point to note, the hard link is consistent with the inode of the source file, which basically means that the hard link and the source file must be in the same file system.

There is also the operation permissions in the second column. It can be seen that the hard link and the source file are also the same, but the soft link is that all users have read and write execution permissions, but this does not mean that all users can change the source file, but only Only operate soft links.

To further explain the difference between soft and hard links, delete the source file directly, and then read the soft and hard links:

$ 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

At this time, the soft link is inaccessible, but the hard link is still accessible (because it is empty, so there is no printing, but no error will be reported. At this time, if you look at the color of the soft link, you can also see that it has changed:

In Linux, a directory is also a file, so theoretically you can create a link for it, but in actual testing, it is found that soft links can be created but hard links cannot be created. The following is an example:

$ 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/: 不允许将硬链接指向目录

You can see that the hard link to create the directory is incorrect. Regarding this point, there is an explanation in "UNIX Environment Advanced Programming":

According to the instructions here, super users can create hard links to directories, but they need the support of the file system itself, but ext4 is currently not supported.

 

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/109130380