What is inode number in Linux
In Linux, whenever a new file is created, it is given a file name and an inode number. This number works as the unique identifier for that file. As a user, you will use the file name to access the file but Linux will first map that filename with an Inode number in a database to access the file. In basic words, an Inode number is just like an index number of a book. You may quickly locate the chapters you wish to read by browsing the index page. You won’t be able to discover the proper information without an index page, and you’ll have to go through the entire book to find a certain subject, which is a complete waste of time. The same applies to inodes in Linux. An inode is a data structure that points refer to the individual blocks, making the file. All of the administrative data required to read a file is included in the inode. The metadata for each file is kept in inodes in a table structure and this data includes:
File types ( executable, block special etc ) Permissions ( read, write etc ) UID ( Owner ) GID ( Group ) FileSize Time stamps including last access, last modification and last inode number change. File deletion time Number of links ( soft/hard ) Location of ile on harddisk. Some other metadata about file.
One thing to keep in mind is that except for the file name and the actual contents, they hold all of the information connected with a file. Inodes are located at the start of the partition. When you refer to a file by name, the system searches for the appropriate inode in the directory entry file where it resides. This provides your system with the necessary information and file data to run any operation.
Why do we need inodes?
Data is saved on your hard drive in fixed-size blocks. If you save a file that is larger than a normal block, your computer will look for the next available segment to save the remainder of the file. That may become difficult and confusing over time. And inodes can help you here. While they do not store any of the file’s actual data, they do save its metadata, which includes all of the storage blocks where the file’s contents may be located. Filenames have no effect on inodes. That means you may duplicate a file, rename it, and it will still refer to the same original inode.
Check Inodes on a Filesystem
You can find a total number of inodes on disk by using ‘-i‘ option with df command. The above command shows the total number of Inodes on /dev/sda1 file system. Also provides the details about used and free inodes. Each filesystem must-have free inodes available to create a new file. If the inodes are full, the system will not allow you to create a new file.
Find Inode number of File
You can find the inode number of any file using -i command line parameter with the ls command. The first field in output is an inode number of the file. You can also search file with an inode number using find command. For example:
Inode Changes with Copy, Move, and Delete
What happens with the inode number when you copy, move or delete a file on the filesystem.
Copy file: cp allocates a free inode number and create a new entry into the inode table.### Check inode of existing file ls -il myfile.txt 1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 myfile.txt ### Copy file with new name cp myfile.txt myfile_new.txt ### Check inode number of new file. Its changed ls -il myfile_new.txt 1150562 -rw-r–r– 1 root root 0 Mar 10 01:09 myfile_new.txt Move or Rename a file: if destination is same filesystem as the source, Has no impact on inode number, it only changes the time stamps in inode table.### Check inode of existing file ls -il myfile.txt 1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 myfile.txt ### Moved file to another directory mv myfile.txt /opt/ ### Check inode number of moved file. No change in inode ls -il /opt/myfile.txt 1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 /opt/myfile.txt Delete a file: Once you delete a file in Linux decrements the link count and free the inode number to be reused.
What to do in case you run out of Inode numbers?
Sometimes, your system may run out of inode numbers, and you may encounter difficulties like no space to write new data, frequent restart, data loss, application freeze, and not being able to log in to the system. In this case, it is recommended to delete the unused files.
Wrapping up
In this article, we learned about inode numbers in Linux and also performed different operations on inodes.