Linux hard link and soft link

Source: Internet
Author: User

1. Linux link concept
Linux links are classified into two types: hard link and symbolic link ). By default, the ln command generates a hard link.

[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 a number no matter what type it is, called the index node number (inode ).
Index ). 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.
You can create a hard connection to important files 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 index node and
For other connections, only when the last connection is deleted will the data block and directory connection 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.

Two hard connection restrictions:

L hard links cannot be created for directories

L a link can be created only between files in the same file system. That is, hard links cannot be established between two files in different hard disk partitions. This is because the hard link points to the original file through the node, and the file I-node may be different in different file systems.

 

[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.

This allows symbolic links (often abbreviated as symlinks) to point to a file located in another partition or even on another network hard disk.

 

2. Deepen understanding through experiments

1 [Oracle @ Linux] $ touch F1 # create a test file F12 [Oracle @ Linux] $ ln F1 F2 # create a hard connection file F23 [Oracle @ Linux] $ ln -s F1 F3 # create a symbolic connection file f34 [Oracle @ Linux] $ LS-li #-I parameter to display inode node information of the file 5 total 06 9797648-RW-R -- r -- 2 Oracle oinstall 0 Apr 21 f17 9797648-RW-r -- 2 Oracle oinstall 0 Apr 21 11 f28 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.

 1 [oracle@Linux]$ echo "I am f1 file" >>f1 2 [oracle@Linux]$ cat f1 3 I am f1 file 4 [oracle@Linux]$ cat f2 5 I am f1 file 6 [oracle@Linux]$ cat f3 7 I am f1 file 8 [oracle@Linux]$ rm -f f1 9 [oracle@Linux]$ cat f210 I am f1 file11 [oracle@Linux]$ cat f312 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 F3 file is invalid.

3. Summary
Therefore, you can perform related tests and draw 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.

 

[Transfer] http://www.cnblogs.com/sonic4x/archive/2011/08/05/2128543.html

 

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.