What is inode and how to view file metadata?

Introduction

Inode, i.e. index node, is an identifier (number) defining the metadata of a file or directory. Metadata is simply additional information (usually irrelevant from the user's point of view, but necessary for the operating system to manage data on storage media).

Metadata includes information such as:

  • File size
  • The medium on which the file is stored
  • The user and group associated with the file
  • Permissions associated with the file
  • Time of file creation and modification
  • Data location (not a filename!)

Metadata does not contain information about:

  • filename given by the user
  • data in the file (entered by the user)

Example

To analyze the metadata, let's create a file called test.txt that contains the text "abc":

echo abc > test.txt

To display the index node, i.e. the number identifying the metadata, add the -i flag to the ls command, while the -l flag will display additional information about the file), e.g.:

ls -li

The displayed result shows that the file has an inode number 264912 assigned to it. We also received information about the creation date, size, etc.

264912 drwxr-xr-x 1 myuser mygroup 4 Jan  13  2024 test.txt

Using the stat command you can display more detailed metadata:

stat test.txt

The result is:

Information/metadata shows the organization of the file on the storage medium.

To find the file associated with a given inode, issue the command:

find -inum 264912

Details

The text "abc" constituting the content of the file did not appear anywhere in the previous screenshot, because the metadata specified by inode only contains information about the size of the file or the organization of data in memory, not the data itself.

Data on disk is stored in the form of blocks of fixed size. If the file size exceeds the block size, the operating system will find another free segment to store the remaining data.

Moreover, inode is also a fixed data structure (see address table and summary below). The inode structure can contain direct pointers to the file's data blocks, but also indirect pointers that are used when the file size exceeds a certain amount of data that can be stored directly in the inode itself.

Źródło

An indirect pointer/indirect block is a mechanism in which the inode contains a pointer to another data structure, which in turn contains pointers to the physical data blocks of the file. This way you can dynamically allocate memory for the file's data as needed as the file grows.

Summary

The inode size in a Linux file system depends on the file system used. For example, for the ext4 file system, the default inode size is 256 bytes. However, depending on your specific file system settings and needs, this size may be adjusted when you create the file system. The larger inode size allows more metadata to be stored directly in the inode structure, which may be beneficial in some cases.

To get information about the inode size (as well as block size and other details) on your file system (in this case /dev/sda1) run the tune2fs command:

sudo tune2fs -l /dev/sda1

Result:


Good luck!

#buymeacoffee