The difference between a Linux soft connection and a hard connection

Source: Internet
Author: User
Tags file system

1.Linux Link Concept

There are two kinds of Linux links, one is called hard link (Hard link), the other is called Symbolic link (symbolic link). The ln command produces a hard link by default.

The specific use is: Ln–s source file Destination file.

"Hard Connection"

A hard connection refers to a connection through an index node. In a Linux file system, a file stored in a disk partition is assigned a number, called an index node number (Inode index), regardless of its type. In Linux, multiple file names point to the same index node. Generally this connection is a hard connection. The effect of a hard connection is to allow a file to have multiple valid pathname, so that users can build hard connections to important files to prevent "accidentally delete" functionality. The reason for this is as described above because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the index node itself and other connections, and only when the last connection is deleted will the file's data block and directory connections be freed. That is, the condition that the file is actually deleted is that all of the hard connection files associated with it are deleted.

"Soft Connection"

Another connection, called a symbolic connection (symbolic link), is also called a soft connection. Soft link files have shortcuts similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains the location information for another file.

Here are two points to note:

1. The ln command maintains the synchronization of each linked file, which means that no matter which place you change, the other files will change the same.

2. LN is linked to soft links and hard links in two kinds, soft link is ln–s * * * *, it will only in the location you selected to generate a file image, does not occupy disk space, hard link LN * * * *, no parameter-s, it will be in the location you selected to generate a file and the same size as the source file, The files remain synchronized, whether they are soft links or hard links. If you use LS to look at a directory, found that there is a file behind a @ symbol, that is a file in the ln command generated, with the ls–l command to view, you can see the path of link displayed.

2. Deepen understanding through practical operation

[oracle@linux]$ Touch F1 #创建一个测试文件f1
[oracle@linux]$ LN F1 F2 #创建f1的一个硬连接文件f2, hard links
[oracle@linux]$ Ln-s F1 F3 #创建f1的一个符号连接文件f3, soft connection
[oracle@linux]$ Ls-li #-I parameter displays the Inode node information of the file
Total 0
9797648-rw-r--r--2 Oracle Oinstall 0 APR 08:11 F1
9797648-rw-r--r--2 Oracle Oinstall 0 APR 08:11 F2
9797649 lrwxrwxrwx 1 Oracle oinstall 2 APR 08:11 f3-> F1

From the results above, it can be seen that the hard connection file F2 and the original file F1 the Inode node is the same, are 9797648, however, the inode node of the symbol connection file is different.

[oracle@linux]$ Echo ' I am F1 file ' >>f1
[oracle@linux]$ Cat F1
I am F1 file
[oracle@linux]$ Cat F2
I am F1 file
[Oracle@linux]$ Cat F3
I am F1 file
[oracle@linux]$ rm-f F1
[oracle@linux]$ Cat F2
I am F1 file
[Oracle@linux]$ Cat F3
Cat:f3:No such file or directory

Through the above test can be seen: when the original file F1 deleted, hard connection F2 is not affected, but the symbolic connection F1 file is invalid

3. Summary (depending on this you can do some related tests, you can get all the following conclusions)

1). Delete Symbol connection F3, have no influence to f1,f2;

2. Delete Hard connection F2, also have no influence to f1,f3;

3. Delete the original file F1, the hard connection F2 has no effect, resulting in symbolic connection F3 failure;

4. At the same time delete the original file F1, hard connection F2, the entire file will be really deleted.

Hard link file has two restrictions

1), not allowed to create a hard link to the directory;
2. Links can only be created between files in the same file system.
The results are the same as soft links when read and write to hard linked files. However, if we delete the source file of the hard link file, the hard link file still exists, and the content is reserved.
At this point, the system "forgot" it used to be a hard link file. and regard him as an ordinary document.


the difference between 22 people

A hard connection is a connection made through an index node. In a Linux file system, a file stored in a disk partition is assigned a number, called an index node number (Inode index), regardless of its type.
In Linux, multiple file names point to the same index node. Generally this connection is a hard connection. The effect of a hard connection is to allow a file to have multiple valid pathname so that the user can establish a hard connection to the important
Files to prevent "accidental deletion" of the function. The reason for this is as described above because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the index node itself and other connections, only when the last
When a connection is deleted, the file's data block and directory connections are released. That is to say, the file is actually deleted.

Soft link files are somewhat similar to Windows shortcuts. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains the location information for another 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.