A glimpse of Linux's distinction between hard connection and soft connection

Source: Internet
Author: User

A glimpse of Linux distinguishes hard connection from soft connection. I. Two Connection Files and creation methods in Linux. There are two Connection Files in Linux: soft connection and hard connection, although they are all connected files, there is a big difference between the two. One is a file (or directory) similar to the shortcut function of Windows. This connection is called a soft connection, and the other is to generate a new file name through the inode connection of the file system, instead of generating new files, this is called a hard connection. The method for creating a connection file is very simple, that is, using the ln command, ln file1 file2 creates a hard connection, file2 is a hard connection of file1, and ln-s file1 file2 creates a soft connection, file2 is a soft connection of file1. Ii. Repeat hard connection in order to better understand the differences between the two connections, let's talk about hard connection first, because it is harder to understand than soft connection. We know that in Linux, each file occupies an inode, and the content of the file is directed by the inode record, and you want to read the file, the file name recorded in the directory must point to the correct inode number to be read. That is to say, the file name is only related to the directory, but the file content is related to inode. So what is hard connection? Create a new file name in a directory to connect to the association record of an inode number. Simply put, multiple file names correspond to the same inode number. Note that multiple file names are used, not multiple files. You may be confused here, so please refer to the example below. Here I have created a hard connection and checked the inode of the relevant files. The result shows that, the inode of these two files is exactly the same (394220), while the number of file connections is not 1 but 2, and the crontab file type is "-" rather than "l ", that is, it is not a connection file because two files are connected to the same file/etc/crontab. The following figure explains why it is so amazing. You 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, the real inode can be found and the final data can be read through the inode. 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, no inode or block is consumed. Maybe you will refute it. No, do you need an inode and blocks for the file that creates/root/crontab? Isn't inode1 used to establish this connection? In fact, this is not the case because the/root directory already exists, and the content in it has already been recorded in this block, that is, no matter whether you add or not add a hard connection inode1 and the block of the directory to which it points already exist, the hard connection you created only adds a file name record to the block pointed to by inode1. In this example, add the/root/crontab record. Therefore, inode and block consumption are not increased. 3. Describe how soft connections are better understood than hard connections (symbolic link). Basically, soft connections are used to create an independent file, this file directs Data Reading to the file name of the file connected to it. That is, only the file is used as the pointing operation. Therefore, when the source file is deleted, the soft connection will "fail", as in Windows, the message "cannot open a file" is displayed ". As shown in the following example, I have created a soft connection. From the output, we can see that it is indeed very different from hard connection. First, the inode numbers of these two files are not the same. In terms of file types, the/etc/crontab is "-", that is, a common file, the/root/crontab_sl parameter is "l", that is, the connection file. What is the content in the crontab_sl file in this example? We can see that its size is only 12B, but its content is only 12 characters including "/etc/crontab. Not quite clear yet? The following figure shows that only the file name is read from inode1 and the inode of the target file is obtained by connecting to the correct directory based on the file name, the correct data can be read. From the above output, we can see that the soft connection consumes inode and block (because the inode number is different from the inode of the source file ), inode1 consumes inode1 and the block it points to. The difference between inode1 and hard connection is that inode1 is not used before the software file is created, it does not refer to any block. It also means that the block it points to is not used. When you want to create a soft connection, inode1 and the block it points to are used. In addition, this block is not used to record the file name, but is a real file, used to record the file information. For example, the information here is the 12 words "/etc/crontab, it is a file stored in the directory/root. Iv. Analysis of Advantages and Disadvantages of soft connection and hard connection 1. Hard connection is secure, because even if the associated data in a directory is deleted, as long as there is associated data in any directory, the file will not be deleted, and hard connection does not require inode and block consumption, but hard connection also has restrictions, that is, you cannot connect to directories across file systems. Note: In a hard connection, When deleting a file, the file can be deleted only when the number of connections is 0. Otherwise, the number of connections of the file will be reduced by 1. 2. The soft connection software is flexible to connect to files and directories, But it consumes inode and block. However, this is nothing for the system, but if the target file is deleted, from the last figure, we can see that the entire process will be unable to continue, and the problem of reading through the connection file will occur. In fact, soft connections are the same as shortcuts in Windows. In short: I think the major difference between hard connection and soft connection is that hard connection will not create a new file, while soft connection will create a new file, this file is used to record the file name of the connected file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.