Go Linux hard links, soft links and inode explanations

Source: Internet
Author: User
Tags parent directory


The file is stored on the hard disk, and the minimum storage unit for the hard disk is called "Sector"(Sector). Each sector is stored 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read each sector, so the efficiency is too low, but the one-time continuous reading of multiple sectors, that is, one time to read a block. This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block.

File data is stored in "blocks", then obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of stored file meta information is called Inode, and the Chinese name is "Index node".

Each file has a corresponding inode, which contains some information about the file.

What does the inode contain?
    • The number of bytes in the file
    • User ID of the owner of the file
    • The group ID of the file
    • Read, write, and execute permissions for files
    • The timestamp of the file, total three: CTime refers to the time when the inode was last changed, mtime refers to the time when the contents of the file last changed, atime refers to the time when the file was last opened
    • Number of links, that is, how many file names point to this inode
    • Location of file data block

       You can use the Stat command to view inode information for a file:

stat test.c
    • 1

In summary, all file information except the filename is present in the inode.

Number of Inode

Each inode has a number, and the operating system uses Inode numbers to identify different files.

It is worth repeating that the Unix/linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify.

  on the surface, the user opens the file by file name. In fact, this process within the system is divided into three steps:
first, the system finds the inode number corresponding to the file name;
second, through the inode number, to obtain the inode information;
Finally, based on the Inode information, locate the block where the file data resides and read the data.

With the Ls-i command, you can see the inode number corresponding to the file name:

  ls -i example.c
    • 1
Catalog files

In a unix/linux system, directory is also a file. Opening the directory is essentially opening the directory file.

The structure of the catalog file is very simple, which is a list of a series of catalog items (dirent). Each directory entry consists of two parts: the file name of the included file, and the inode number that corresponds to the file name.

The LS command lists only all the file names in the directory file:

 ls /etc
    • 1

The ls-i command lists the entire directory file, the file name and inode number:

 ls -i /etc
    • 1

If you want to view the details of a file, you must access the Inode node based on the inode number and read the information. The ls-l command lists the details of the file.

-l /etc
    • 1

Understanding the above knowledge, you can understand the permissions of the directory.
  The Read permission (r) and Write permission (W) for the directory file are for the directory file itself. Because only the file name and inode number are in the directory file, if you have Read permission, you can only get the file name and cannot get additional information because the other information is stored in the Inode node, and the information inside the Inode node requires execute permission (x) of the directory file.

Hard Links

In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name. However, the Unix/linux system allows multiple filenames to point to the same inode number.

This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. this is referred to as a "hard link".

The ln command can create a hard link:

  ln 源文件 目标文件
    • 1

After running the above command, the source file is the same as the inode number of the destination file, pointing to the same inode.
  One of the inode information is called the "number of links," which records the total number of filenames pointing to the inode, which increases by 1.

Conversely, Deleting a file name causes the "number of links" in the Inode node to be reduced by 1. when this value is reduced to 0, indicating that no file name points to the Inode, the system reclaims the inode number and its corresponding block region.

  here, by the way, the "number of links" to the catalog file.
When you create a catalog, two catalog entries are generated by default: "." and ".".
  The inode number of the former is the inode number of the current directory, which is equivalent to the "hard link" of the currentdirectory, and the inode number of the latter is the inode number of the parent directory of the current directory, equivalent to the "hard link" of the parent directory.
  Therefore, the total number of "hard links" in any directory is always equal to 2 plus the total number of subdirectories (with hidden directories).

The Unix file system provides a mechanism for linking different files to the same file, which we call a link.
It allows a single program to use a different name for the same file. The benefit is that the file system only has a copy of the file.
The system simply implements this connection by creating a new entry in the directory. The registration entry has a new file name and inode number for the file to connect to (the Inode is the same as the original file).
No matter how many hard links a file has, there is only one inode on the disk that describes it, so long as the number of links to the file is not 0, the file remains there. Hard links cannot establish hard links to directories!

  A hard connection is built directly on the node table (inode), and the count value above the node table is updated when a hard connection is established to a file .
For example, a file is connected two times (hard connection), the value of this file is 3, and regardless of the 3 file name of any one of the access, the effect is exactly the same, but if you delete any of them, only the value of the calculation minus 1, does not delete the actual content, (Any existing file itself is a hard connection) only when the count value becomes 0 and no hard connections point to the actual deletion of the content.

Hard Connection Summary:

1. Multiple files with the same inode number are hard to connect files to each other.

2, delete the hard connection file or delete any one of the source files, the file entity has not been deleted.

3, only delete the source files and all the corresponding hard connection files, the file entity will be deleted.

4, when all the hard connection files and source files are deleted, and then the new data will occupy the space of this file, or the disk fsck check, the deleted data will be collected by the system.

5, the hard connection file is another entrance to the file.

6. You can prevent important files from being mistakenly deleted by setting hard connection files to files.

7, by executing the command "ln source file Hard Connection file", you can complete the creation because of the connection.

8. Hard connection files can be deleted by RM command.

9, for the static file (no process is called file), when the corresponding number of hard connections is 0 (i_link), the file is deleted. I_link View Method (the third column of the ls-l result is)

Variables for file deletion:

Number of hard connections to the I_link file

I_count reference count (with one program using I_count plus 1)

Conditions for file deletion:

I_link=0 and I_count=0

Soft joins and hard-link plots

Soft links

In addition to hard links, there is a special case.

The inode number for file A and file B is different, but the content of file A is the path to file B. When you read file A, the system automatically directs the visitor to file B. Therefore, regardless of which file you open, the final read is file B. At this point, file A is referred to as the "soft link" of File B (soft link) or "Symbolic link" (symbolic).

This means that file a depends on file B and if file B is deleted, opening file A will cause an error: "No such file or directory".
  This is the biggest difference between soft links and hard links: file a points to file B's file name, not file B's inode number, and file B's Inode "link count" does not change.

The ln-s command can create a soft link.

-s 源文文件或目录 目标文件或目录
    • 1

We refer to the symbolic link as a soft link, which is a special file that points to another file whose data part contains only the pathname of the file to which it is linked.
Soft links are introduced to overcome the lack of hard links, and soft links do not directly use the Inode number as a file pointer, but instead use the file path name as a pointer (soft link: file name + data part –> The pathname of the destination file).

The software has its own inode and has a small space on the disk to store the path name. Therefore, the soft link can cross file system, also can and directory link! Second, a soft link can link to a nonexistent file name, but it will not open its link until it has been created.

The special role of the Inode

Because the inode number is separated from the file name, this mechanism causes some unix/linux system-specific phenomena.

1. Sometimes the file name contains special characters and cannot be deleted properly. At this point, delete the Inode node directly, you can play the role of deleting files.

2. Move or rename the file, just change the file name without affecting the inode number.

3. After opening a file, the system will identify the file with the Inode number and no longer consider the filename. Therefore, it is generally not possible for the system to know the file name from the inode number.

The 3rd makes software updates simple and can be updated without shutting down the software, without requiring a restart. Because the system through the Inode number, identify the running files, not through the file name. Update, the new version of the file with the same file name, the generation of an inode, will not affect the running files. The next time you run this software, the file name will automatically point to the new version of the file, the old file's inode is recycled.


The exact meanings of hard links and soft links are as follows:

We use the following instructions to hard link the Ppp.h file to the original file proc.h

ln proc.h ppp.h

The Inode node of the original proc.h file is 516134, which points to the first file in the data area. After using the LN directive, create a new directory entry in directory, with the file name Ppp.h,inode node still 516134, pointing directly to the same proc.h.

We experimented on the Linux system to verify that our explanation was correct, that the inode nodes of the two files were the same, pointing to the same file:

We use the following instructions to link the Ppp.h file to the original file proc.h

Ln-s proc.h Ppp.h

(GO) Linux hard links, soft links, and inode details

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.