Working With inodes in Linux
Inodes in Linux are entries that form the basis of file systems in Linux. They manage and handle metadata about files and facilitate the inner working of files in Linux. File systems in Linux have two sections; the inodes and data blocks.
Inodes act as data structures, defining directories and files on the Linux file system. Moreover, they get stored in the directory entry. An inode contains all data needed to read files and point to blocks that make up files. Therefore, programs that refer to files by their name look up the corresponding inode from the directory entry file.
All files in Linux directories contain an inode number and the filename, and using the inode number; one can retrieve the file’s metadata. However, the inode numbers and file names get stored in a separate index but link to the inodes.
An inode contains different information, including:
- File permissions
- File size
- Group and User IDs of a file
- Timestamps for creating, reading, and writing a file.
- Location of the data
- Hard and soft links
- File types
Checking inode Numbers
You can check the number of inodes in a file system or even for a specific file. To check for a file system, use the command below.
In the output below, you can note the specific file system and its different statistics, including free and used percentages.
Alternatively, you can specify a specific file system to check. For instance, to check for /sda5, we can use the command below.
To check the inode number for a specific file, you can list its contents using the ls -il or stat command. The stat command displays the statistics of a given file, such as the inode number. For our example, our file is test.txt, and our command is shown below.
Other file details, such as the creation date, get displayed with the stat command.
Similarly, listing the contents of the current directory displays the number of blocks of a specific file.
For this case, the inode number is 4983306.
To check the directory-level inode, add the -d flag to the command of checking the inode of files.
The new command will be:
Having a count of the inode usage is beneficial as excessive inode usage is problematic and can hinder creating newer files even when there is available space on the file system. To maintain a low inode usage, use the tricks listed below.
- Clear or delete cache files
- Delete old email files
- Remove unneeded directories and files
- Delete temporary files
Doing so ensures you don’t run out of inodes and have enough room to create more files. Some effects of running out of inodes vary, but the common ones include:
- Processes not restarting
- Periodic tasks not loading
- Crashing of applications
- Data loss
- Unable to create files or directories.
When you start seeing such signs, check the number of inodes and try the above tips to maintain low inode usage.
Wrap Up
Understanding inodes when working with Linux file systems is essential. We’ve covered the concepts and commands you should know when working with inodes in Linux. Understanding inodes give you an upper hand when examining file systems and maintaining low inode usage. Hopefully, this article covered all you need to know about inodes in Linux.