Linux: Check Inode Usage

Objective: Get the inode usage count and the number of free inodes on a Linux system. Also learn how to find out the inode number of a file and search for a file using a inode number.

Inode is a data structure in a Unix-style filesystem that describes a file-system object such as a file or a directory. It may contain metadata information like file access time, permissions, etc.

Each filesystem or partition has its own set of inodes. To uniquely identify a file, you need both the inode and the device partition information.

If all the inodes in a filesystem are used up, the kernel can not create new files even when there is available free disk space. So, if you have ample free disk space but unable to create new files, check the inode usage for that partition.

To get the inode usage for the root partition using df, use the following syntax.

$ df -i /
Filesystem      Inodes  IUsed   IFree IUse% Mounted on
/dev/nvme0n1p5 7692288 652294 7039994    9% /

On ext2/ext3/ext4 filesystems, you can also use the tune2fs command.

$ sudo tune2fs -l /dev/nvme0n1p5 | grep -Ei 'inode count|free inode'
Inode count:              7692288
Free inodes:              7040110

Tips and Tricks using Inode

To get the inode of a file, ls command can be used. For example, to get the inode for the /etc/hosts file, use the following syntax.

$ ls -li /etc/hosts
3932325 -rw-r--r-- 1 root root 223 May  8  2020 /etc/hosts

The inode number for the /etc/hosts file is 3932325.

If you want to search for a file belonging to a specific inode, use the find command. Let’s do a reverse search for the /etc/hosts file using the inode number 3932325.

$ find / -inum 3932325 -xdev 2> /dev/null
/etc/hosts

For the example above, we are telling find to search the root partition and not to descend to directories on other filesystems using the -xdev option. Without the -xdev option, find will check for a file with the specified inode number on all partitions.

If you are running out of inodes and need to determine which directory is hogging up most of the inodes, you can use the following syntax.

$ { sudo find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n; } 2>/dev/null
...
    97 /var/log
  2260 /usr/bin
  9130 /var/lib/dpkg/info

We can see from the output above that /var/lib/dpkg/info directory has about 9000 files. You can proceed to cleanup the directory or move the contents of that directory to another partition to free up the inodes.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }