Detailed Linux soft connection and Hard link _linux

Source: Internet
Author: User

This article details the Linux soft connection and hard links, nonsense not to say, and then look down.

A linked file

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

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

Linked files can even link nonexistent files, which produces what is commonly called a "broken chain" problem (or "phenomenon"), linked files can even be linked to the loop itself. Similar to recursion in a programming language.

A soft connection can be generated with the Ln-s command, as follows:

[root@linux236 test]# ln-s source_file softlink_file

When you read or write a symbol file, the system automatically converts the action to the source file, but when you delete the linked file, the system simply deletes the linked file without deleting the source file itself.

PS: Add soft connection to Directory

1, whether the source file address or destination file address must use absolute path, otherwise there will be "the number of symbolic connections too many" error

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

The Info ln command tells you that a hard link is another name for a pre-existing file (A "Hard link" is another name to an existing file), which is somewhat confusing. The Hard connection command is

 
 

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 the two

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.

Deepen understanding through experiments

[oracle@linux]$ Touch F1     #创建一个测试文件f1
[oracle@linux]$ ln F1 f2     #创建f1的一个硬连接文件f2
[oracle@linux]$ ln-s F1 F3    #创建f1的一个符号连接文件f3
[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 or Acle 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

In this way 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.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.