The Ln command is used to create links between files. By default, it is used to create hard links (hard links cannot be created for directories) and use option-s to create symbolic links. Before talking about ln, let's take a look at the links in linux.
1. Links in Linux are also called link files. They can be divided into hard links and symbolic links. The difference between the two is that the hard link points to the file itself, and the symbolic link points to the file name. This difference is very important. It leads to the differences in the nature and operation of the two linked files, which are reflected in various aspects. I will summarize the differences at the end of this article.
2. How can we understand the two different points of the two linked files? If you understand the file storage mechanism in linux, this is a good understanding. I mentioned in my previous inode article that in Linux, file names, file attributes, and file content are stored separately, so hard links and symbolic links can have different points.
Here we will talk about it. As I have said before, in linux, the file search sequence is: locate the inode number based on the file name, and locate the inode block based on the number, then, locate the data block (File Content) based on the attribute information in the inode block ). Therefore, the hard link points to the file itself, which is actually the inode Number of the file. The reason why it is not explicitly stated that it is pointing to the inode number is because it is too far away and involves more concepts, which is even more unclear, therefore, a non-conceptual file is used instead.
3. Some people say that linux linked files are similar to windows shortcuts. To some extent, this is the case. Strictly speaking, it is different. First, symbolic links, hard links, and windows shortcuts all point to the function. However, the differences between them are also obvious: the windows shortcut points to the file location, both symbolic links and hard links are different from windows shortcuts. Secondly, in terms of the nature of files, symbolic links and windows shortcuts are both files, however, hard links are not a type of file, but a type of point.
Why is a symbolic link a file, but a hard link is not? This is because when a link is created, the system will re-allocate an inode number for the symbolic link, but will not re-allocate the inode number for the hard link. The hard link shares an inode with its source file. As mentioned above, every time a file is created in linux, the system will allocate an inode to it. Each file has a unique inode number. We can determine from this perspective whether they are a file. Run the ls-li command to view their inode numbers. As I will talk about below, the hard-link file m_h shares an inode with its source file mial.
The so-called file hard link is not a kind of file. It is a common file instead of a Window shortcut named itself as a file. Interesting?
4. When we use the ls-li command to view the file, the system provides the file information by field. According to the info help file of the ls command, the first field is the inode number, the second field is the file type and permission information, and the third field is the number of hard links of the file. What is the number of hard links?
Let's look at an image.
We have created a symbolic link m_s and a hard link m_h for the mail file ., The first field is the inode number. We can see that the hard-linked mail and m_h inode are the same. The first character of the second field is d, which is a directory file and-is a common file, if it is l, It is the mail link file m_s. The third field is the number of hard links, the hard links of the five files in the image are 5, 2, 2, 1, 1, and 4.
The number of hard links is the literal translation of "number of hard links" in the ls info help document. This is what is translated in most articles. According to regulations, the number of hard links of common files without hard links is 1. For each added hard link, the number of hard links increases by 1; for directories without sub-directories, the number of hard links is 2, each time a sub-directory is added, the number of hard links increases by 1.
So what is the number of hard links? Why is there another strange rule? I have a little bit of understanding, but it is not necessarily correct. Let's talk about it here. Thank you for your patience.
The info help document of the ln Command says: a 'hard link' is another name for an existing file. I think the number of hard links is the number of file names. More specifically, I think the number of hard links actually points to the file itself.
A common file without a hard link has only one point to the file, so the number of hard links is 1, add a point to the file itself, so add 1 to the number of hard links. The symbolic link points to the file name and does not point to the file itself. Therefore, no matter how many symbolic links are created for the file, the number of hard links does not increase.
So, why is the number of hard links in the directory file that does not contain subdirectories 2? This is because there are only two directory items in the directory file that does not contain subdirectories: "." and ".", which represent the directory itself and its parent directory. Each time a sub-directory is added, a directory item must be added to the directory file. Then create a subdirectory under the subdirectory, that is, create a subdirectory of the current directory. Will the directory items of the current directory be added? No.
Now let's take a look at the meaning of the number of hard links in the five files in the image: they represent three subdirectories under the desktop directory, one hard link for the mail file, and one hard link for the m_h file, the M_s file does not have a hard link. The XX directory contains two subdirectories.
5. What are the specific differences between the two links?
First, the conceptual hard link points to the file itself, and the symbolic link points to the file name. Secondly, hard links are not a type of files, and symbolic links are. Then, in terms of the relationship with the source file, the hard link is closely related to the source file. modifying any file can cause changes to another file. The Symbolic Link is more like an independent file, you can modify the symbolic link and the source file it points. In addition, the hard link is not explicitly a hard link; the symbolic link explicitly indicates that it is a symbolic link. At last, hard links can save a lot of disk space, but they cannot create hard links for directories or cross-file systems. Symbolic Links are much more flexible than hard links.
You may wonder: is m_h obviously larger than M_s? How can hard links save disk space than symbolic links? This is because m_h displays the mail size, which basically does not occupy disk space. Run the du command.
The directory xy contains the mail file (12 K) and its hard link file. Use du to count the disk space occupied by the Directory. It is found that it occupies 16 K, the mail file is 12 K, and the remaining 4 K is the directory item. It seems that m_h is not counted at all. In fact, it is not counted, but its size is 0. With option-l, you can see it by enabling the repeated statistics function, the disk space occupied by directory xy is changed to 28 KB.
Over ~ After talking so much, I hope I can understand them.