Linux Index nodes and links

Source: Internet
Author: User

Linux Index nodes and links

in the learning of Linux, the index node and link concept is a simple and important basic concept, I learn the two concepts of the summary, here to share to everyone, if there are inappropriate, please the great God message reminder, thank you!


1.0 Linux Index nodes

In a Linux file system, no matter what type of file is saved in the disk partition, the system assigns a number to it, called Index node. In Linux, there are multiple files pointing to the same inode, which is commonly referred to as hard-link "tough link."

    an index node (inode) is a table entry that contains information about the file (metadata), Includes: file type, permissions, Uid,gid, number of hard links (pointing to the number of path names for this file), the size of the file and the different timestamp, the data block pointer to the file on disk, and other data about the file. Use the Stat command to view the

[[Email protected] testdir]# stat filename File: ' filename ' size:38 blocks:8 IO block:4096 Regular Filede vice:805h/2053dinode:134 links:1access: (0755/-rwxr-xr-x) Uid: (0/root) Gid: (0/root) context:unconf ined_u:object_r:etc_runtime_t:s0access:2016-07-24 21:54:56.948084045-0400modify:2016-07-24 21:53:30.720370973- 0400change:2016-07-24 21:54:37.683701323-0400 Birth:-[[email protected] testdir]#

The above display file name (file) is: filename;

Size: 38 bytes (normally shown as bytes, if you want to visually view the file size, use the command ' ll-h filename ');

Number of blocks (Blocks): 8;

Single IO device block size (io Blocks) 4096 bytes;

File type: Rugular file (normal);

Index node (inode): 134;

Number of hard links (link): 1;

Permissions (Access): (0755/-rwxr-xr-x);

Uid & Gid: (0/root) & (0/root);

Three timestamps: access:2016-07-24 21:54:56.948084045-0400

Modify:2016-07-24 21:53:30.720370973-0400

Change:2016-07-24 21:54:37.683701323-0400


650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/85/27/wKiom1ebCGPx-4tLAAJriP8KEBo309.png-wh_500x0-wm_3 -wmp_4-s_1371525211.png "width=" "height=" 466 "title=" inode diagram. png "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 750px;height:466px; "alt=" Wkiom1ebcgpx-4tlaajrip8kebo309.png-wh_50 "/>


The inode structure is a

The inode's metadata is stored in a manner similar to that of human memory-the memory palace:

1, when the stored metadata is not very large, exactly within the capacity range of 12 direct block pointers, the source data is stored in 12 direct block pointers;

2. When the size of the stored raw data exceeds the capacity of 12 direct block pointers, the 13th block indirect block pointer is enabled: the second block acts as a block holding multiple pointer directories, each pointing to the next block, and the next block being the block that holds the original data. Both the double-indirect block pointer and the triple-block pointer work like this.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/27/wKioL1ebDNbg5CZ3AAHxq4vHKHs547.png-wh_500x0-wm_3 -wmp_4-s_1041603575.png "title=" inode table "width=" "height=" 436 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:750px; height:436px; "alt=" wkiol1ebdnbg5cz3aahxq4vhkhs547.png-wh_50 "/> People refer to a file by file name, and the file reference is through an inode number, So a directory is a mapping between the file name and the inode number in the directory.


1.2 cp RM and Inode relationship


1.2.1 CP and Inode

CP command:

Allocate a free inode number to generate a new entry in the Inode table

Create a catalog entry in the directory, associating the name with the inode number

Copy data to generate a new file

1.2.2 RM and Inode

RM command:

The number of links is decremented so that the inode number released can be reused

Put data blocks in the free list

Delete a catalog entry

Data is not actually deleted immediately, but when another file uses a block of data

will be overwritten.


1.2.3 MV and Inode

If the target and source of the MV command are in the same file system as the MV command

Create a new directory entry with a new file name

Delete old directory entries corresponding to older file names

Does not affect the Inode table (except timestamp) or data location on disk: No

Data is being moved!

If the target and source are in a different file system, MV is equivalent to CP and RM



2.0 Linux Links


In Linux systems, there are two types of Linux links: Hard links (hardlink) and soft links (symbolic link)


2.1 Hard Links


In a Linux file system, no matter what file is saved on disk, the system automatically assigns an inode number, and multiple filenames point to the same inode number, which is called hard link.

The role of a hard link is that an inode number can have more than one valid path. such as:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/85/28/wKioL1ebFcWwFMjLAABBCx7lG2U828.png-wh_ 500x0-wm_3-wmp_4-s_412475369.png "title=" hard link Graph "width=" "height=" 560 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 750px;height:560px; "alt=" wkiol1ebfcwwfmjlaabbcx7lg2u828.png-wh_50 "/>    for important documents, It is necessary to create a hard link. Because when the source file is modified, the corresponding hard-link file is also modified, and when the original file is deleted, the corresponding hard-link file is not affected. That is, all hard-linked files associated with the file are deleted. (ll-i filename to view file filename  inode number)

[Email protected] testdir]# ll-i/hard1/hard.link 7304-rw-r--r--. 2 root root 0 Jul 04:43./hard17304-rw-r--r--.  2 root root 0 Jul 04:43/hard.link[[email protected] testdir]# echo Test >/hard.link [[email protected] testdir]# Cat./hard1test[[email protected] testdir]# rm-f./hard.link [[email protected] testdir]# cat./hard1test

Note that a hard link is a physical file that corresponds to the same file system, so creating a hard link must be under the same partition or cannot span the drive, otherwise it cannot be done.

When you delete a file:

Link for RM command decrement count

File to exist, at least one connection number

When the number of links is zero, the file is deleted


2.1 Soft Link (symboliclink) ln-s original file path soft link

Another connection is called a symbolic connection (Symboliclink), also called a soft connection. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

A soft link file differs from a hard-link file:

Do not share source file inode;

Delete the source file, the corresponding soft link file can not be used, delete the soft link file, the source file may also be used;

Can be created across drives or not under the same partition;


Path-the original file relative path: Relative to the soft-connect file path, not to the current working directory






Linux index nodes and links

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.