"Go" linux Soft connect hard link

Source: Internet
Author: User

Original link http://www.cnblogs.com/yfanqiu/archive/2012/06/11/2545556.html

There are two special "files" for soft links and hard links in Linux systems.

A soft link can be thought of as a shortcut in Windows that lets you quickly link to a target file or directory.

Hard links generate new files through the inode of the file system, rather than generating new files.

The creation method is simple:

    1. Soft link (symbolic link) ln-s source target
    2. Hard link (entity link) ln source target

"Copy command can also create soft connections, hard links"

Inode

To explain clearly the difference between the two and the connection need to first clear the Linux file system Inode this thing. When partitioning and formatting partitions, the entire partition is divided into two parts, the Inode and data block (where 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.

The number of inode is usually arranged according to the purpose of the partition (this is another topic), such as a large number of files and small files, you need to increase the inode large so that all files can be indexed. Otherwise, the partition is not full and cannot be written to any files.

Catalog files and archive files

Catalog file: Record the file name under this directory

Archive: Record actual file data

The inode itself does not record the file name and the file name is recorded in the block of the directory file, so adding, deleting, and changing the file name is related to the W permission for the directory. So when we want to read a file, we must go through the inode and block of the directory before we can find the inode number of the file to be read, and finally read the data in the correct file Block . the system locates each file through an index node rather than a file name.

Directory Inode (Permissions are met?) ) = Directory block and file Inode (to satisfy permissions?) ) = File Block

Hard Links

Multiple file names correspond to the same inode, and a hard link simply adds a file name to the associated record that links to an inode number in a directory. If you delete any of the files, the Inode and the block are still present, and you can still read the correct profile data through another file name. In addition, regardless of which file name to edit, the final result will be written in the same inode and block, so the data can be modified.

Soft connection

A soft connection is the creation of a separate file, and this file will let the data read to its link file name, because only as a point of action , so when the source file is deleted, the soft connection file cannot be opened, because the original file name cannot be found. 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.

Here is an example of a hard link and a soft link.

Now there are two files in the directory, one is called AAA, and the other is called BBB.

$ ls-il

963922-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 AAA

963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB

First make a hard link to AAA.

$ ln AAA Aaahard

$ls-il

963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 AAA

963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 Aaahard

963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB

Here we note that before creating a link, the number of links displayed by AAA is 1, after the link is created

The number of links between 1.AAA and Aaahard becomes 2.

The inode numbers for the 2.AAA and Aaahard are the same, all 963922.

The 3.AAA and Aaahard display the same file size, which is 92B.

The results of the operation of the LN command are visible:

AAA and Aaahard are two names of the same file , they have the same index node number and file attributes,

A hard link to file AAA is to create a new pointer on the current directory for the AAA File Index node.

You can delete any one of them, such as RM AAA, and delete only one pointer at a time, and the number of links minus one at a time.

The kernel removes the file contents from disk only if all pointers to the contents of the file, that is, the number of links, are reduced to 0 o'clock.

Although hard links save space and are the traditional way for Linux systems to integrate file systems,

But there are some shortcomings:

1. You are not allowed to create hard links to directories.

2. It is not possible to create links between files in different file systems.

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.

Then we do a soft link to the BBB, soft link overcomes the lack of hard links, no file system restrictions,

Any user can create symbolic links to directories.

It is now more widely used, it has greater flexibility,

You can even link files across different machines and networks.

$ ln-s BBB Bbbsoft

$ ls-il

Total dosage 0

963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 AAA

963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 Aaahard

963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB

963924 lrwxrwxrwx 1 Ocean Ocean 3 2007-05-18 15:47 Bbbsoft-BBB

From the results of the link above can be seen in soft links and hard links , the difference is not only conceptually, in the implementation is also completely different.

Difference:

1. Hard link Original file/link file common an inode number, stating that they are the same file,

The soft link original file/link file has a different inode number, indicating that they are two different files;

2. On the file properties, the soft link explicitly writes out the link file, and the hard link is not written.

Because in essence the hard link file and the original document are completely equal relations;

3. The number of links is not the same, the number of links to soft links will not increase;

4. File size is not the same, hard-link file display size is the same as the original file.

The size of the soft link shown here is different from the original file, the BBB size is 95B, and the Bbbsoft is 3 B. Because BBB has a total of 3 characters

5. soft links do not have any file system restrictions, any user can create a symbolic link to the directory

In short, creating a soft link is the creation of a new file .

When accessing a linked file, the system will find that he is a link file that reads the link file to find the file that is actually being accessed.

Of course, soft links also have the disadvantage of hard links:

Because the link file contains the path information of the original file, when the original file is moved from one directory to another directory,

Then access the linked file, the system can not find, and hard links do not have this flaw, you want to move how to move;

And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.

"Go" linux Soft connect hard link

Related Article

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.