The difference between hard links and soft links in Linux

Source: Internet
Author: User

One of the most important features of the Linux file system is his file link, which is a reference to the file that allows the file to be seen in many places in the file system. Links can be treated just like the original file, and when edited through a link, it is exactly the same as editing the original file directly. In Linux, links are divided into two types. One is a hard link, and one is a soft link (symbolic link).


The first thing to know is that when partitioning and formatting partitions, the entire partition is divided into two parts, the Inode and data block (the actual data is placed in the data region), which is the unique identifier (directory, archive) file in a file system. When you need to access this file, you must first locate and read the inode of the file. The Inode stores many important parameters of the file, the unique identifier is called Inumber, the other information has the creation time (CTime), the modification Time (mtime), the file size, the owner, the attribution user group, the read and write permission, the data block number and so on.


In the Linux file system, the files stored in the disk partition are assigned an I node number, the I node represents the corresponding inode, through the I node to find the location of the files on the disk (block block).


Hard Links:

different files in Linux point to the same I node, which is a hard link. A hard link is a link through an index node (inode), and a hard link is a file that can have a different path or a different name. So the hard link file and the original file is actually the same file.


Create method: ln filename linkname


Hard-Link Features:

1, the hard link file and the original file has the same I node and block blocks, can be regarded as the same file;

Can image of the hard link file and the original file is understood as a file of two different interfaces, is the same storage space. Like two different doors in a classroom, you can go straight to the classroom.

2, through the I node to identify hard links;

Because the hard links and the original files point to the same I node, their I node numbers are exactly the same. Similarly, hard-link files are also more covert, it is the existence of another identity of the file, and other files exactly the same. Hard links can be found only by looking at the I node number. To view the I node number, we can use the "ls-il" command to implement,

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/26/wKioL1ebBPmgMCcZAAAYW3WZJ24724.png "title=" 1.PNG " alt= "Wkiol1ebbpmgmcczaaayw3wzj24724.png"/>

You can see that F1 and Q1 are exactly the same, and they differ from other files in that their number of nodes is 2, which indicates that they are hard-linked files. So the more hard links are created for the same file, the number of links is incremented.

3. Cannot be used across partitions

Hard-link files must correspond to a physical file on the same file system and cannot be used across partitions. Because the Inode is the index value of the file in the current partition, it is relative to this partition, and of course it cannot cross the file system. The simple understanding is that the same file or storage space must be under the same partition. It's like the same classroom has to be in the same building upstairs. No longer the same building, of course, it is impossible to enter the same classroom through two different doors.

4. Cannot be used for directory

Use:

Because hard links are pointing to the same inode, creating a hard link is creating a new pointer to the inode of the original file in the current directory, so you can delete any one without affecting the other use at all. Because of having the same I node and block blocks, it is pointing to the same file and modifying the file data will change.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/27/wKiom1ebCyvhRVUOAAAv0ZasX-Q504.png "title=" 2.PNG " alt= "Wkiom1ebcyvhrvuoaaav0zasx-q504.png"/>

Whether you edit F1 or Q1, you can see f1,q1 exactly the same.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/27/wKiom1ebDJ-Ba7MeAAAaAEqc6zg363.png "title=" 3.PNG " alt= "Wkiom1ebdj-ba7meaaaaaeqc6zg363.png"/>

Delete one, the other can be used normally, the number of links is reduced. If the file is to exist, there is at least one link number and the file is deleted when the number of links is zero.


Soft Links:

A soft connection is the creation of a separate file, which has separate inode and block blocks, except that the file name and I node numbers of the linked files are recorded in his block blocks, and then the file data is found through the inode of the source file. Soft links can be understood as a quick way.


Create method: Ln-s filename linkname


When creating a soft link, be aware that the path to the original file is relative to the path of the soft link file, not the path of the current directory, if you use a relative path!

First case: Direct write original file name --Error

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/28/wKioL1ebHwmDlTrRAAAXne-gjFk732.png "title=" 6.PNG " alt= "Wkiol1ebhwmdltrraaaxne-gjfk732.png"/>

The second case: the original file name writes relative to the current directory relative path --Error

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/28/wKiom1ebH72im3TPAAAWhpXtA78517.png "title=" 7.PNG " alt= "Wkiom1ebh72im3tpaaawhpxta78517.png"/>

Third case: The original file name is written relative to the soft-linked file- -correct

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/29/wKiom1ebIICAzsrYAAATp8L2b_k819.png "title=" 8.PNG " alt= "Wkiom1ebiicazsryaaatp8l2b_k819.png"/>

Fourth case: Write the absolute path of the original file- -correct

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/29/wKioL1ebI-eRUgbxAAASUIbhpkw995.png "title=" 9.PNG " alt= "Wkiol1ebi-erugbxaaasuibhpkw995.png"/>


Soft Connection Features:

1, similar to Windows shortcuts;

exist completely independently, just the path to another directory or file under the current directory.

2, the soft link has its own I node and block blocks, but the data block only save the original file name and I node number, and no actual file data;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/28/wKioL1ebF0WAv28fAAAXTeuC5kY988.png "title=" 4.PNG " alt= "Wkiol1ebf0wav28faaaxteuc5ky988.png"/>

You can see the soft links created W1 to Q1, the I node numbers for W1 and Q1, the permissions, and the sizes are different. And W1 Filenames are also specifically identified, so they are independent of two files

3, lrwxrwxrwx l Soft link Soft link file permissions are rwxrwxrwx

Because a soft link is just a pointer to an action, the final permission depends on the permissions of the source file. So the permissions of soft links don't matter.

4, modify any file, the other changes;

Because a soft link can be understood as a shortcut, modifying either one will change the other.

5, delete the original file, soft links can not be used;

Since the source file was deleted, the soft-connected file could not be opened because the original file name could not be found because it was only a pointer to the action. The content of the link file is only a file name, linked to the correct directory according to the file name to further obtain the inode of the target file, and finally be able to read the correct data. If the original file name of the target file is deleted then the whole link will not go down.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/28/wKioL1ebGonw-gUbAAAYbvCZgXY228.png "title=" 5.PNG " alt= "Wkiol1ebgonw-gubaaaybvczgxy228.png"/>

The soft link file will be specially identified, the source file pointed to will be represented and flashing, the soft link file is not available!


This article is from the "I ' m Groot" blog, so be sure to keep this source http://groot.blog.51cto.com/11448219/1831829

The difference between hard links and soft links in Linux

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.