Tutorial on inode usage in Linux
This article mainly introduces you to the relevant information about inode in Linux. The introduction in the article is very detailed and has a certain reference and learning value for everyone. Friends who need it can take a look below.
Background
I was reviewing Linux commands recently, and when I went to df, I discovered something that I had ignored before. That is, the -i option lists the inode information of the file system partition. What is this inode?
What is inode used for?
Inode is an area used to store file meta information. The Chinese translation is called "index node".
Background knowledge about inode
Let’s first review some contents of file storage. We know that files are stored on the hard disk. The smallest storage unit of the hard disk is also called a sector. The size of a sector is 512 bytes.
When the operating system reads information on the hard disk, it reads multiple sectors at once, and these multiple sectors are also called blocks. Typically, the block size is 4KB, approximately 8 sectors in size. It should be noted that the blocks read are contiguous spaces.
At this time we can know that files are stored in "blocks", just like when we write a C language program, we know that when we declare an array, not only will it be stored The value placed in the array will also store the corresponding array information, such as the first address of the array, file type, array length, etc. Similarly, you need to find a place to store the meta-information of the file, similar to the information related to file creation. , the length of the file, etc. And this place is called inode.
The content stored in the inode
The inode contains the meta-information of the stored file, including these contents:
The number of bytes in the file.
The ID of the file creator.
The Group ID of the file.
File read and write permissions.
The relevant timestamp of the file. There are three specific ones: ctime-->The time when the inode was last changed; mtime-->The time when the file content was last changed; atime-->The time when the file was last opened.
Number of links
Block position of file data
inode number
After seeing the above storage content for the first time, I think everyone will have the same question. Since inode stores file-related information, why not store files? The name. The reason is that file names are not the standard for Unix/Linux operating systems to identify different files.
The operating system identifies different files through inode numbers.
In Unix/Linux systems, the user level name is used to open the file through the file name. The system level mainly goes through three steps to open the file:
-
Find the corresponding inode number based on the file name.
Get inode information through inode number.
According to the inode information, find the block where the file data is stored, and store the data alone.
The special function of inode
In the Unix/Linux system, the inode number and the file name are separated. This has led to some special phenomena in the system:
Deleting the inode node means deleting the file. Some files may not be deleted correctly. In this case, we can directly delete the corresponding inode node to delete the file.
Move files or rename files without changing the inode number, just the file name.
Generally speaking, the system cannot obtain the file name through the inode number. When a file is opened, the system will identify the file through the inode and no longer consider the file name.
Because of the existence of the inode number, the system can be updated without closing the software. The system identifies the running file through the inode number. During the update process, the file exists with the same file name and a new inode without affecting the currently running file. The original old version of the inode will be recycled the next time the software is opened, and the file name will automatically point to the new inode number.
Inode space occupation problem
Since the data is also stored in the hard disk, the inode will definitely occupy the hard disk space. When formatting the hard disk, the operating system will automatically divide the hard disk into two areas:
data area
inode table
The data area mainly stores file data, and the inode table area stores inode information.
Specially, the size of the area occupied by the inode is already given by the operating system when the disk is formatted. The consequence of this is that the space in the data area has not been used up, but data cannot be accessed anymore. At this time, because the inode table area is full, new files cannot be stored on the disk.
Directory file
We know that in Unix/Linux, any resource exists in the form of a file. So does the catalog. When we open a directory, we actually open the directory file. The structure of a directory file is a list.
Directory entry = file name of the included file + corresponding inode number.
Hard links and soft links
I won’t cover the specifics of what is a hard link and what is a soft link in this blog post I won’t go into details, just consider it from the inode perspective.
Considering from the perspective of inode number, in Unix/Linux system, multiple file names are allowed to point to the same inode number. At this time, if one of the file names is deleted, access to the other file name will not be affected. At the same time, if the file is opened through one file name and modifications are made, the modifications can be shared when other file names are opened. Then call this a "hard link". In Linux, we can create hard links through the ln command.
As summarized above, in the inode, there is a storage item called "number of links", which records the total number of file names that only want the inode. If a file name is created to point to a file through a hard link, the link number in the inode data field corresponding to the file will be + 1, and vice versa - 1. When this value is 0, the system will default to no file name pointing to the inode. At this time, the inode number will be recycled and the corresponding block area will be recycled.
As for the corresponding soft link, suppose there is file A and file B, and B is the soft link of A. At this time, the inode numbers of A and B are different because they are different files, but! The content of B is the path of A. When reading B, the system will automatically access A, so no matter which file is opened, file A will be accessed. At this time, file B is called a "soft link" or "symbolic link" to file A.
In Unix/Linux systems, we can create soft links through the ln -s command.
Summary and small additions
Through the above description, we know that inode is like the pointer field in C language, the pointer field It records a variety of information and directs us to the correct file location to read the required information. (Of course it is not exactly the same.)
When creating a directory in a Unix/Linux system, two directory entries will be automatically generated:
.Directory
.. Directory
You can observe these two directories through the ls -al command. The inode number of ".directory" is the inode number of the current directory, which is equivalent to the hard link of the current directory, while the inode number of the ".." directory is the inode number of the parent directory of the current directory, which is equivalent to the hard link of the parent directory. Total number of directory hard links = 2 + total number of subdirectories (including hidden files).
The above is the detailed content of Tutorial on inode usage in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

Huobi to Binance Transfer Guide: Safe and conveniently transfer your crypto assets Many investors use Huobi and Binance at the same time. This article will guide you how to safely transfer crypto assets on Huobi (HTX), such as TRUMP and USDT, to the Binance platform. Binance is popular for its high security, rich currency and trading pairs, and world-leading trading volume. Binance Exchange’s advantages: the world’s number one trading volume, accounting for 50% of the global market; transparent reserve assets are mainly mainstream stablecoins such as Bitcoin, Ethereum and USDT; it has effectively avoided the potential risks of the US SEC and is one of the most stable and reliable exchanges at present. This tutorial will take TRUMP and USDT as examples
