There are two types of Connection Files in Linux: one is a file similar to the shortcut function of windows, which allows us to quickly connect to the target file, called symbolic link ); the other is to generate a new file name through the inode connection of the file system, instead of generating a new file, which is called hard link ).
1. Hard Link (hard connection or actual connection)
In a Linux file system, each file occupies one inode, and the file content is directed to (Block) by inode records ). To read this file, you must point to the correct inode number through the file name of the directory record. That is, the file name is only related to the directory, and the file content is related to inode. Hard link refers to the association record where multiple file names correspond to the same inode number. It only creates a new file name under a directory to connect to an inode number.
The actual connected file is read as follows:
We can find two different file names through the block specified by inode in the 1 or 2 directory. No matter which file name is used, we can refer to real's inode to read the final data. The biggest benefit of doing so is security. If you delete any file name, inode and block still exist. In this case, you can use another "file name" to read the correct file data. In addition, no matter which file name is used for editing, the results can be written to the same inode and block.
In general, when hard link is used to set the connection file, the disk space and inode quantity will not change. It only writes one more associated data to the block in a directory. In special cases, when you want to add this data to a block, it will be full, which will lead to changes in disk space. However, the associated data used by hard link is usually very small, so it usually does not change the size of inode and disk space.
Note: hard link can only be performed in a single file system.
Moreover, you cannot connect to a directory. This is because when you connect to a directory, the connected data needs to be connected together with all the data under the connected directory.
2. Symbolic Link)
It is also called a soft connection. It is used to create an independent file, which enables data reading to point to the file name connected to it. After the source file is deleted, the symbolic link cannot be opened.
Read the symbolic connection file as follows:
Inode 1 reads only the file name to the connection file, and connects to the correct directory based on the file name to obtain the inode of the target file. Finally, the correct data is read.
Symbolic Link occupies inode and block.
Note that when you change the symbolic link file, the source file is actually changed.
A soft connection can be used across file systems or directories.
Instance:
When you create the hard connection link1 and soft connection link2.ln commands of the linktest file without any parameters, a hard connection is created by default.
It can be seen that the properties of the hard connection file and the source file are identical, including the inode number.
Delete the source file below and find that the hard connection can still be read, but the soft connection cannot be read.