Deep understanding of hard links and soft links

Source: Internet
Author: User
Article Title: in-depth understanding of hard links and soft links. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Both UNIX and Linux provide the link command ln. As the name suggests, ln links two file names (which is not accurate) to each other so that users can access the same file regardless of the file name they are using. This is what we want to achieve with the ln command, but technically, only soft links are links to two file names, and hard links are not included in this column. To thoroughly understand the meaning of a link, you must distinguish between a file and a file name. To distinguish between files and file names, we need to know how the file system manages files. The technical background of seemingly Common commands is not simple at all.

How to manage files in a file system is too broad, and it is impossible and not necessary to describe all of them here, but we only need to know the following technical points. In Linux, files are extended. In common sense, "files" (such as a document) are files, directories, devices, and so on. These files are diverse and contain a large number of files. What should I do in case of duplicate names? How does the file system differentiate and manage them? Limit duplicate names? Of course not! Let's take a look at how Linux works. In the original Linux file system, each file was assigned a data structure called an index node (Inode), which contains a unique index node number throughout the system, the file system uses this index node number to identify a file. In addition, all information used by the file system to manage files is included in the index node.

All right, assign a unique number to each file, and the file system will know whether a file is a file named Michael Jacob or a file named Michael Lee. But the real users of the file-do we humans also need to use this number to identify the file? Well, if this is true, I don't know whether it's a computer or a computer player. Therefore, we need to differentiate each file in our way and give each file a name that is easy to remember. The same location (that is, the same directory) cannot have two files with the same name. If the location is different, duplicate names are allowed. At this point, humans have methods to identify files. Computers (file systems) have computers. Oh, the problem is coming again. The two are "Fire and water, or "unlimited communication? This is another question, of course, the latter.

OK. Now that we need to "communicate", the "Link" will come up naturally. After a big circle, I finally got my words back to the ground. With the background knowledge above, whether it's hard links or soft links, it's already fish meat on the board. The so-called hard link (also called link [1]) is one or more file names of a file. In other words, the so-called link is nothing more than linking the file names used by humans with the node numbers used by computers (file systems. Therefore, we can link multiple file names to the same file. These file names can be in the same directory or different directories. A file has several file names (multiple file names are implemented using the ln command). We can say that the number of links to this file is several. The number of links can be 1, indicating that the file has only one file name.

Note that the hard links mentioned in info ln help information are different from my understanding. Let's first look at how info talks about hard links. "A" hard link "is another name for an existing file; the link and the original are indistinguishable. "(a" hard link "is the alias of an existing file; the linked file cannot be different from the linked file .) Wow, it seems like a mess. who is right and who is wrong? No one is correct. They have different perspectives. Info is a "hard link" in the ln command. Note that the hard link in info is also double quotation marks. I think this has a special meaning. "Linked files and linked files cannot be different." That's right, because they all point to the same index node. The main difference between info and I is that I agree with info and I do not agree with the following viewpoint: for a file with a file name, there is still a hard link to the file without using the ln command, and the number of links is 1. Theory requires practice to prove that ls is used in Linux? L command to obtain detailed information about the files in this directory, in the following format:

-Rwxr-xr-x 1 root 1024 Apr 30 filename

In the above format, "1" with a bold underline indicates that the number of links to the file named filename is 1. Make an experiment and run the ln command in the same directory (different directories are also supported:

Ln filename filename2

Then use ls? L command to obtain the following information:

-Rwxr-xr-x 2 root 1024 Apr 30 filename

-Rwxr-xr-x 2 root 1024 Apr 30 filename2

Obviously, the file with the original file name filename now has two file names, and the number of links is changed to 2. All other information is not changed, therefore, "a linked file cannot be different from a linked file ". The following facts make me doubt whether it is necessary to use ln to generate a new hard link: No matter whether any file name is used to change the file, all direct or indirect [2] link files of this file will change accordingly, and all information will be consistent at last.

Separate the file name from the index node, and then use the hard link technology to make file management more convenient and more efficient. For example, to rename a directory, a process does not need to open the file. You only need to modify the content of a directory. This is also true for deleting a file. The number of links to the file is reduced by 1. If the number of links to the file is zero after the directory is deleted, the system then deletes the real file from the disk.

It is easier to understand the two restrictions of hard links.

1. Hard links cannot be created for directories;

2. A link can be created only between files in the same file system.

The length relationship will not be repeated.

To overcome the preceding restrictions, symbolic link (also called soft link) is introduced ). Symbolic Links are actually a special file type, which contains any path name of another file. This path name points to any file in any file system, and can even point to a non-existent file. The system automatically changes most operations on symbolic links (such as reading and writing) to operations on source files, but some operations (such as deleting) will be completed directly on the symbolic links. Add option-s in ln to generate a symbolic link to the file. In short, a hard link is used to allow multiple files not in the same directory or in the same directory to modify the same file. After one of the files is modified, all files with hard links have been modified. The soft link is the same as the shortcut key in the window.

Related Article

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.