I’m going to ask you a question. What do you see when you pass the ls -l command? A list of files with a bunch of details like permission, number of files, file owner, group owner, size, date & time along with a file name, and more number of data can be accessed using different parameters, but have you ever imagined where all data get stored, and what we call for this data type?
This type of data is called meta-data, which is useful to store all information of files except file name and data of a file. And meta-data is a part of an inode. Now, what is an inode?
Of course, this question will arise? And to resolve this, we are there for you. In this article, you will learn what an inode is and how to find inode numbers in Linux.
What is an Inode number in Linux
There is plenty of information available on the internet about what Inode number is. Personally, I didn’t get it much, so I’ll avoid that abstract concept.
In simple terms, Inode number is like an Index number of your Linux file system, similar to the index page of our book. When you go through the index page, you can easily find the content that you want to read. Without an index page, you will not be able to find the right content, and you have to read the whole book to find a specific topic which is totally a waste of time. Undoubtedly, you will be yelling at the author.
The same concept goes around the inode number. If you have noticed the name inode, I stand for Index, and the rest you know well.
Inode number has a data structure that holds all the metadata information, except the file name, and of course the data of file, assigned with a unique inode number which help files system to keep track of inode usage.
Moreover, inode number limitations can be set while creating a file system and don’t get confused with actual storage and inode capacity. You will find many users complaining about “No space left on device” despite of available space.
The reason for the above issue is the inode number, and once the inode reaches a threshold level, the kernel ignores the request to create a new file.
How to find or access inode number
Above we’ve mentioned that inode number stores metadata, but what kind of metadata information does it hold exactly?
For that, you can use ls or stat, as per the documentation stat command is recommended way to get details about specific files, as follows,
- Size: A size of the respective file
- Bocks: number of blocks allocated
- File type: is it directory,regular,pipe,socket,character, block
- Device: in which disk data in a hexadecimal format
- Inode number: yes, we found it
- No. Links: of hard links present on the system for specific file
- Permission attributes: show the specific permission attributes
- Owner & Group information of file: information about owner & group
- Timestamp: it holds various timestamps such as actime,mtime, change, and creation time.
Check number of available inode in Linux
There are many commands that can be used to find the number of inodes available to use and how much you’ve already utilized and left to use.
Check available inode number using df command
I suggest you to use
df command, which can be more than enough to get information about inode capacity for specific disk partitions.
If you want an exact number of inodes available in your system, use the below code, and you change the partition name as per your system.
$ df - /
I’m not good with counting numbers. If you too, use the below code.
$ df -ih /
Check available inode number using tune2fs command
This may be a good option if you want to have more information about the disk, but right now, your motive is to find inode information for specific disk partitions.
Pass this below command to check the total inode count and free inodes.
$ sudo sudo tune2fs -l /dev/sda10 | grep -Ei 'free inodes|inode count'
I’m not sure whether this tool will work on btfrs, lvm, or any other filesystem. As per the manual, tune2fs will work flawlessly on the ext2/ext3/ext4 filesystem. Please let me know if I’m wrong.
Check number of inode for specific file
From the above method, you can check the inode availability for specific disk partition and other important inode details, but you cannot grab the inode number for any specific file. For that, you have to use other Linux commands such as
Let us see how to use the above-mentioned command to get an inode number.
Check number of inode for specific file using ls
The most common command you can use for getting the inode number of a particular file is the
-i parameter. To get an inode number, pass the below command and ensure to replace the file name.
$ ls -i FILE-NAME.txt
The output will show you the inode number along with a file name.
Check number of inode for specific file using stat
When you do man for an inode, it will show stat command information to retrieve metadata, which includes an inode number, and many other important informations.
Pass the below code and check for the inode pair to get inode information.
$ stat FILE-NAME.txt
For the precise output, you can use the below code, and this will make sure to print only inode numbers and numbers of hard links present on your system.
$ stat FILE-NAME | grep -i inode | cut -c20-
Find number of hardlinks present by inode
From the above output, you have seen two hard links are present on my system, but how to know where it is stored exactly. To find that hard links file pass the below command.
$ find [DIRECTORY-WHERE] -inum [INODE-NUMBER]
Find the total number of Inode used by directories
There may be situations where you want to find a total number of inodes used by current directories. To find that you can use
find with other utilities.
find . -printf "%h\n" | cut -d/ -f-2 | sort | uniq -c | sort -rn
When inode number cannot be changed
Inode number cannot be changed under two circumstances when creating a hard link or moving a file. Apart from that, any modification on the filesystem can change the inode number.
How to free inode number incase of runs out
There can be a possibility when your system runs out of inodes, and you may face issues like frequent restart, data loss, no space to write new data, application freeze, and the system may not allow you to log in.
To prevent this, just delete the unused file from your system.
There is much to know about inode numbers, Although we tried to cover all the aspects of inode with a simple example and best example. Did we perform well?
You may be interested to know the Types of Linux File Systems.
If you found this article helpful, let us know in a comment section.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.