Do you really know about inode in Linux?

Time:2021-7-20

introduction

Inode is a very important knowledge point of Linux, understanding it is conducive to our further understanding of Linux system.

Today, I happened to see a blog note made by Ruan Yifeng on inode. Here I simply record what I have learned.

concept

Storage unit:

  1. Sector, 512 bytes (equivalent to 0.5kb)

  2. A block is composed of eight sectors

File storage is divided into two parts

  1. data storage

  2. File meta information

File meta information is inode

Inode content

The metadata of inode file includes the following contents:

  1. The number of bytes in the file

  2. The user ID of the file owner

  3. The group ID of the file

  4. Read, write and execute permissions of files

  5. There are three timestamps for a file: CTime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, and atime refers to the time when the file was last opened.

  6. The number of links, that is, how many file names point to this inode

  7. Location of file data block

View inode information

stat 1.txt

View the total number of inodes and the number of used hard disks

df -i

Inode number

Each file will have an inode number, which is equivalent to the table ID in the MySQL database and is used to find data. Each file name corresponds to an inode number. When we open a file, we actually go through the following steps:

  1. Find the corresponding inode number through the file name

  2. Obtaining inode information through inode number

  3. Find the block where the data is located through the inode information and read the data

View the inode number of the file / directory

ls -i 1.txt

Special role

Due to the separation of inode number and file name, this mechanism leads to some unique phenomena in UNIX / Linux system.

1. Sometimes, the file name contains special characters and cannot be deleted normally. At this time, deleting the inode node directly can delete the file.

2. Moving or renaming a file only changes the file name and does not affect the inode number.

3. After opening a file, the system identifies the file by inode number, regardless of the file name. Therefore, generally speaking, the system cannot know the file name from the inode number.

The third point makes it easy to update the software. It can be updated without shutting down the software. Because the system through the inode number, identify the running file, not through the file name. When updating, the new version of the file will generate a new inode with the same file name, which will not affect the running file. The next time you run the software, the file name will automatically point to the new version of the file, and the inode of the old version will be recycled.

The end of the article

Adhere to learn a little every day, knowledge needs to accumulate, come on ~ share with you~

reference resources

Personal blog

original text

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Programming Xiaobai must understand the network principle

How is the network composed? Why can we easily surf the Internet now?Whether you are a computer major or not, you may always have such questions in your heart!And today we will solve this matter and tell you the real answer! Basic composition of network First, let’s look at this sentence Connect all computers together […]