Tutorial on inode usage in Linux

Aug 09, 2017 pm 03:10 PM
inode linux Tutorial

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

See all articles