linux link file (suitable for novices to learn)

Linux links include hard links and soft links (symbolic links)

1. Hard link

Under normal circumstances, the file name and the inode number are in a "one-to-one correspondence" relationship, and each inode number corresponds to a file name. However, Unix/Linux systems allow multiple file names to point to the same inode number. This means that the same content can be accessed with different file names; modification of the file content will affect all files; however, deleting one file name will not affect access to another file name. This situation is called a "hard link".

[root@localhost ~]# echo 123456 > file1
[root@localhost ~]# ll -i file1                           #-i:显示inode编号
68960174 -rw-r--r--. 1 root root 7 7月  27 14:55 file1
[root@localhost ~]# ln file1  file1-test
[root@localhost ~]# ll
总用量 12
-rw-------. 1 root root 1201 6月  16 10:04 anaconda-ks.cfg
-rw-r--r--. 2 root root    7 7月  27 14:55 file1
-rw-r--r--. 2 root root    7 7月  27 14:55 file1-test
drwxr-xr-x. 2 root root    6 7月  27 14:55 test
[root@localhost ~]# ll -i file1 file1-test               #查看inode号
68960174 -rw-r--r--. 2 root root 7 7月  27 14:55 file1
68960174 -rw-r--r--. 2 root root 7 7月  27 14:55 file1-test
[root@localhost ~]# rm -rf file1                         #删除源文件
[root@localhost ~]# ll -i file1-test                     #查看链接文件
68960174 -rw-r--r--. 1 root root 7 7月  27 14:55 file1-test
[root@localhost ~]# cat file1-test                       #查看链接文件内容
123456

After running the above command, the inode numbers of the source file and the target file are the same and point to the same inode. There is an item in the inode information called "number of links", which records the total number of file names pointing to the inode, and will be increased by 1 at this time. Conversely, deleting a file name will reduce the "number of links" in the inode node by 1. When this value decreases to 0, indicating that there is no file name pointing to this inode, the system will recycle the inode number and its corresponding block area.

Note: Hard links 
1. cannot cross file systems
2. Directories are not supported as hard links

2. Soft connection

In addition to hard links, there is a special case:

Although the inode numbers of file A and file B are different, the content of file A is the path of file B. When reading file A, the system will automatically direct the visitor to file B. Therefore, no matter which file is opened, file B is ultimately read. At this time, file A is called a "soft link" or "symbolic link" of file B.

This means that file A depends on file B for existence. If file B is deleted, an error will be reported when opening file A: "No such file or directory". This is the biggest difference between soft links and hard links: file A points to the file name of file B, not the inode number of file B. The inode "link number" of file B will not change as a result.

The ln -s command can create soft links

Syntax: ln -s source file link file

[root@localhost ~]# echo qwer > file2
[root@localhost ~]# ll -i file2
68960171 -rw-r--r--. 1 root root 5 7月  27 15:42 file2
[root@localhost ~]# ln -s file2 file2-test                  #将文件file2软链接到file2-test
[root@localhost ~]# ll file2-test
lrwxrwxrwx. 1 root root 5 7月  27 15:43 file2-test -> file2
[root@localhost ~]# ll -i file2-test file2                  #查看inode号
68960171 -rw-r--r--. 1 root root 5 7月  27 15:42 file2
68960177 lrwxrwxrwx. 1 root root 5 7月  27 15:43 file2-test -> file2
[root@localhost ~]# cat file2
qwer
[root@localhost ~]# cat file2-test
qwer
[root@localhost ~]# rm -rf file2-test                       #取消软连接
[root@localhost ~]# ln -s file2 file2-test
[root@localhost ~]# rm -rf file2                            #删除源文件
[root@localhost ~]# ll file2-test
lrwxrwxrwx. 1 root root 5 7月  27 15:45 file2-test -> file2 #已失效

#给目录设置软链接必须是绝对路径
[root@localhost ~]# ln -s /root/aaa/ /usr/bbb
[root@localhost ~]# ll /usr/bbb
lrwxrwxrwx. 1 root root 10 7月  27 15:58 /usr/bbb -> /root/aaa/
[root@localhost ~]# rm -rf /usr/bbb                         #取消链接,注意:删除目录链接时目录后面加“/”是删除目录,不加是删除链接

Make multiple links to some important files

The difference between soft links and hard links:

- Soft links can cross file systems, but hard links cannot;
- Soft links can connect directories, but hard links cannot;
- After deleting the source file, the soft links become invalid, and hard links have no effect;
- Both types of links can be connected through commands ln to create;
- ln creates a hard link by default;
- Use the -s parameter to create a soft link.

Guess you like

Origin blog.csdn.net/qq_54494363/article/details/131960207