Differences between linux soft connection and hard connection

Source: Internet
Author: User

Differences between linux soft connection and hard connection

1. Linux link concept

Linux links are classified into two types: Hard Link and Symbolic Link ). The ln command generates hard links by default.

The specific usage is the target file of the ln-s source file.

[Hard connection]

Hard connection refers to the connection through the index node. In a Linux File System, a file stored in a disk partition is assigned with an Index node number (Inode Index) No matter what type it is ). In Linux, multiple file names direct to the same index node. Generally, this type of connection is hard connection. A hard connection allows a file to have multiple valid path names. In this way, you can create a hard connection to an important file to prevent "accidental deletion. The reason 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 connection between the index node and other nodes. Only when the last connection is deleted will the connection of the data block and directory of the file be released. That is to say, the true condition for deleting a file is that all hard connection files related to the file are deleted.

[Soft connection]

Another connection is also called Symbolic Link. Soft-link files have shortcuts similar to Windows. It is actually a special file. In a symbolic connection, a file is actually a text file containing the location information of another file.

Note the following two points:

1) The ln command will keep the synchronization of each link file. That is to say, no matter which one you change, other files will change the same.

2 ). there are two types of ln links: Soft link and hard link. The soft link is ln-s *****. It will only generate a file image at your selected location, it does not occupy disk space. The hard link ln *** has no parameter-s. It will generate a file of the same size as the source file at the selected position, both soft links and hard links are synchronized. If you use ls to view a directory, you will find that some files are followed by a @ symbol, that is, a file generated using ln command, and you can use ls-l command to view it, the link path is displayed.

2. Deepen understanding through actual operations

[Oracle @ Linux] $ touch f1 # create a test file f1
[Oracle @ Linux] $ ln f1 f2 # create a hard connection file f2 of f1 with a hard link
[Oracle @ Linux] $ ln-s f1 f3 # create an f1 symbolic connection file f3, soft connection
[Oracle @ Linux] $ ls-li #-I parameter displays inode node information of the file
Total 0
9797648-rw-r -- 2 oracle oinstall 0 Apr 21 08:11 f1
9797648-rw-r -- 2 oracle oinstall 0 Apr 21 08:11 f2
9797649 lrwxrwxrwx 1 oracle oinstall 2 Apr 21 f3-> f1

From the above results, we can see that the hard connection file f2 is the same as the inode node of the original file f1, both of which are 9797648. However, the inode node of the symbolic 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

The test above shows that after the original file f1 is deleted, the hard connection f2 is not affected, but the symbolic connection f1 file is invalid.

3. Conclusion (based on this, you can perform some relevant tests and draw all the following conclusions)

1) Delete the symbol to connect to f3, which has no effect on f1 and f2;

2). Deleting a hard connection f2 does not affect f1 or f3;

3) deleting the original file f1 does not affect the hard connection f2, resulting in invalid symbolic connection f3;

4). Delete the original file f1 at the same time and hard connect f2. The entire file will be deleted.

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.