In-depth understanding of hard links and soft links
Unix
with
Linux
Both provide a link command
ln
. As the name suggests,
ln
Is the name of two files
(
This statement is not very accurate
)
Link each other so that users can access the same file regardless of which file names you use. This is our use
ln
The order to achieve, but from the technical statement, only the soft link is the link two file names, hard links are not in this column. To make a thorough understanding of the meaning of the link, you must distinguish between files and file names. To distinguish between files and file names, we have to know how file systems manage files. It seems very ordinary order, its technical background is not simple.
The file system management file involves too much scope, it is impossible to do not need to describe it, but we only need to know the following technical points. In Linux, the extension of the file is large, and the "document" in our knowledge (such as a document, etc.) is the file, directory, equipment, etc. are also files. These have a wide variety of files, the number is huge, what is the name of the name, how is the file system distinguishes and manages them? Limit the weight? Of course! Let's take a look at how Linux is doing. The original Linux file system assigned a data structure we called the index node (inode) for each file, there is a full system unique index node number in this index node, the file system is relying on this index node number to identify A file. In addition, all information used to manage files is included in the index node.
Ok, assign a unique number of Number to each file, the file system knows that a file is Zhang San's file or a document of Li Si. But the true user of the file - Do we use this Number to identify files? Oh, if this is true, I don't know if people play computer or computer play. So we must use our way to distinguish all files, give each file a nice name, the same place (ie the same directory) does not have two names. If the location is different, the rename is allowed. At this point, the method of human identification documents, computer (file system) has computer practices, oh, the problem is coming, the two are "water is not allowed", or "Communication Unlimited"? This is also asking, of course, the latter.
OK, since "communication", "link" will naturally be mentioned. Wrought a large circle, finally got home to attack, with the background knowledge above, whether it is a hard link or soft link for us is already a pair of fish. The so-called hard link (Hard Link, also known as link [1]) is one or more file names of a file. By talking about the white point, the so-called link is nothing more than the file name and computer (file system) used by our human beings. So we can link with the same file with multiple file names, which can be in the same directory or different directories. A file has several file names (using the ln command to implement multiple file names), we say that the number of links to the file is a few. As can be seen from the definition, this link may be 1, indicating that the file has only one file name.
It is important to point out that the hard links in the help information of Info LN are different from my understanding, first see how INFO is hard link. "A" Hard Link "is another name for an existing file; the link and the original are indistinguishable." Is an alias for existing files; link files are unable to distinguish between link files.) Wow Oh, as if the set is, who is wrong? No one is right, the two angles are different. Info is "hard link" on the LN command, pay attention, the hard link in Info is also playing double quotes, I estimate that there is a special meaning. "The link file is unable to distinguish by the link file", it is right, because they all point to the same index node. Now I am mainly different from info is that I agree that INFO does not agree with the following perspective: For a file with file name, the file still has a hard link, and the number of links is 1. The theory needs practice to prove that in Linux, use the ls -l command to get the details of the file in this directory, the format is as follows: -rwxr-xr-x 1 root root 1024 APR 30 17:06 FileName
In the above format, the link of the "1" represents the file name file name filename is 1 in the file name filename. Do an experiment, use the ln command in the same directory (different directories):
Ln filename filename2
Then use the ls -l command to get the following information:
-rwxr-xr-x 2 root root 1024 APR 30 17:06 Filename
-rwxr-xr-x 2 root root 1024 APR 30 17:06 filename2
Obviously, the file named filename has two file names, and the number of links is 2. In addition, all other information is different, so "The link file is not different from the link file". " The following facts let me suspect that there is a need to generate new hard links with LN: No matter any file name to file, all direct or indirect [2] link files of the file will change, the last information They are all consistent.
Separate the file name with the index node, then use hard link technology to make the management file more convenient and efficient. For example, rename, the process does not need to open the file, just change the content of a certain directory item. This is also the same, the corresponding directory item is deleted, and the number of links of the file is reduced. If the number of links of the file is zero after deleting the directory item, the system will remove the real file from the disk.
I know how hard links are, and the two restrictions on hard links are easier.
1, not allowed to create hard links to the directory;
2, only the link can be created between files in the same file system.
Target relationship, no longer repeat it.
In order to overcome the above restrictions, the symbolic link (Symbolic Link, also known as soft link). The symbolic link is actually a special file type, which contains any path name of another file. This path name points to any file located in any file system, can even point to a file that does not exist. The system will automatically turn most of the operations such as read, write, etc., such as read, write, etc.) to the source file, but some operations (such as deletion, etc.) are done directly on the symbolic link. Add option -s in the LN to generate a symbolic link for a file.
[1] The link is generally understood as hard links before discussing the symbolic link. [2] If ln file1 file2; ln file2 file3, I call file3 is the indirect link file of File1.