The introduction

Inode is one of the most important knowledge points in Linux.

Today, I accidentally saw a blog note on inode made by Teacher Ruan Yifeng. I will briefly record what I learned here.

concept

Storage unit:

  1. Sector, 512 bytes (0.5 KB)

  2. A block consists of eight sectors

File storage is divided into two parts:

  1. Data is stored

  2. File meta-information

File meta-information is the inode

The inode content

The inode meta-information contains the following information:

  1. The number of bytes of the file

  2. The User ID of the file owner

  3. The Group ID of the file

  4. Read, write, and execute permissions on files

  5. Ctime indicates the time when the inode last changed, mtime indicates the time when the file content last changed, and atime indicates the time when the file was last opened.

  6. The link count is how many file names point to the inode

  7. Location of the file data block

View inode information

stat 1.txt
Copy the code

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

df -i
Copy the code

The inode number

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

  1. Find the inode number by file name

  2. Obtain inode information by inode number

  3. Inode information is used to find the block where the data resides and read the data

View the inode number of a file or directory

ls -i 1.txt
Copy the code

Special role

Because inode numbers are separated from file names, this mechanism results in some phenomena unique to Unix/Linux systems.

1. Sometimes, the file name contains special characters and cannot be deleted. In this case, the inode node can be deleted directly to delete the file.

2. Move or rename a file. The inode number is not affected but the file name is changed.

3. After a file is opened, the system identifies the file by inode number, regardless of the file name. Therefore, in general, the system does not know the file name from the inode number.

Point 3 makes it easy to update software without shutting it down, without rebooting it. Because the system identifies running files by inode number, not by file name. When updated, a new inode is created with the same file name, without affecting the running file. The next time you run the software, the file name automatically points to the new version of the file, and the inode of the old version is recycled.

Article end

Insist on learning a little every day, knowledge is need to accumulate over a long period of time, come on ~ share with you ~

reference

Personal blog

The original