There are two types of Linux links, one is called Hard Link and the other is called Symbolic Link. By default, the lncommand produces a hard link. Linux link
Hard link
Hard links refer to connections through an inode. In a Linux file system, a file stored in a disk partition is assigned a number, called the Inode Index, regardless of its type. In Linux, multiple file names pointing to the same index node exist.
For example, if A is a hard link of B (A and B are both file names), the inode node number in the directory entry of A is the same as the inode node number in the directory entry of B, that is, one inode node corresponds to two different files.
Name, two file names point to the same file, A and B are completely equal for the file system. Deleting any of them will not affect the access of the other one.
The role of hard links is to allow a file to have multiple valid path names so that users can establish hard links to important files to prevent "false deletions".
The reason for this is as described above, because there are more than one connection to the index node corresponding to the directory. Deleting only one connection does not affect the inode itself and other connections. Linux link
Only when the last connection is deleted, the file's data block and directory connections are released. That is to say, the condition that the file is actually deleted is that all hard link files associated with it are deleted.
Soft connection
Another type of connection is called a symbolic link, also called a soft connection. Soft link files have shortcuts similar to Windows. It is actually a special file. In a symbolic link, a file is actually a text file that contains location information for another file.
For example: A is a soft link of B (A and B are both file names), the inode node number in the directory entry of A is not the same as the inode node number in the directory entry of B, and A and B point to two different Inode, which in turn points to two different blocks of data.
However, the data block of A is only the path name of B (you can find the directory entry of B according to this). There is a "master-slave" relationship between A and B. If B is deleted, A still exists (because the two are different files), but it points to an invalid link.
2. Deepen understanding through experimentation
[ oracle@Linux ] $ touch f1 #Create a test file f1 [ oracle@Linux ] $ ln f1 f2 #Create a hard link file f2 of f1 [ oracle@Linux ] $ ln - s f1 f3 #Create a symbolic link to f1 File f3 [ oracle@Linux ] $ ls - li # -i parameter display file inode node information total 0 9797648 - rw - r -- r -- 2 oracle oinstall 0 Apr 21 08 : 11 f1 9797648 -
Rw - r -- r -- 2 oracle oinstall 0 Apr 21 08 : 11 f2 9797649 lrwxrwxrwx 1 oracle oinstall 2 Apr 21 08 : 11 f3 -> f1
As can be seen from the above results, the hard link file f2 is the same as the inode node of the original file f1, both are 9797648, but the inode node of the symbolic link file is different.
[ oracle@Linux ] $ echo "I am f1 file" >> f1 [ oracle@Linux ] $ cat f1 I am f1 file [ oracle@Linux ] $ cat f2 I am f1 file [ oracle@Linux ] $ cat f3 I am f1 file [ oracle@Linux ] $ rm - f f1 [ oracle@Linux ] $ cat f2 I am f1 file [ oracle@Linux ] $ cat f3 Cat : f3 : No such file or directory
Through the above test, it can be seen that the hard link f2 is not affected after deleting the original file f1, but the symbol connection f1 file is invalid.
3. Summary
Here you can do some related tests and get all the following conclusions:
- 1). Delete the symbol connection f3, no effect on f1, f2;
- 2). Delete the hard link f2, and have no effect on f1, f3;
- 3). Delete the original file f1, has no effect on the hard connection f2, causing the symbol connection f3 to be invalid; Linux link
- 4). At the same time delete the original file f1, hard link f2, the entire file will be deleted.
0 Comments