Reading Time: 6 minutes

As you are probably already aware, everything is considered to be a file in Linux. That includes hardware devices, processes, directories, regular files, sockets, links, and so on. Generally, the file system is divided into data blocks and inodes. With that being said, you can think about inodes as a basis of the Linux file system. To explain it more clearly, an Inode is a data structure that stores metadata about every single file on your computer system. 

In our day-to-day interactions with clients, we often find that many do not fully understand all the intricacies that go into disk space manipulation and utilization. This usually means providing in-depth information on how much available disk space is on a hard drive and how it is being utilized. 

In this article, we will delve into what an inode is, what it contains, how to check available inodes, and lastly, how to clean up inodes. Let’s get started!

What Does an Inode Contain?

The inode depicts a file-system object like a directory or file. An inode stores attributes about that object, including the block location on the disk, metadata, ownership, and permission data. Since every file has an inode assigned to it automatically as soon as it is created, each inode is identified by a unique number. This figure is called the ‘index number.’ The following information or metadata is stored within each inode. 

  • Size of the file
  • Owner of the file
  • Date/time when the file was last accessed or modified
  • Permissions and access control of the file
  • The physical location of the file on the disk
  • The file type
  • Number of links associated with the file

As you can see, everything except the name and actual data is located within a single inode. The total number of inodes we will have at our disposal depends on how the hard drive was formatted.

Now that we know that an inode in Linux is a data structure assigned to each file upon creation and stores metadata. Let’s see how to check your inodes and clear them through the command-line interface.

Check and Clear Inodes in Linux

First, let’s check the information on our home/ directory using a simple command called stat.

[root@host /]# stat home/
  File: ‘home/’
  Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 131074 Links: 8
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-11-15 20:07:11.044052460 +0200
Modify: 2020-10-11 15:44:39.925434606 +0300
Change: 2020-10-11 15:44:39.925434606 +0300
 Birth: -
[root@host /]# 

In the example above, we used the stat command on our home directory. Here, you can see all the information that we discussed. It contains the type of file, index or inode number, timestamps of last access, last modified date, the date of last change, and permission settings and ownership. The access, modify, and change information is defined as:

  • Access - the last time the file was read
  • Modify - the last time the file contents were modified
  • Change - the previous time metadata (permissions) of the file was altered

Now let’s check the total number of inodes that we have on our hard drive. This can be accomplished using a straightforward command

[root@host /]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 482591 320 482271 1% /dev
tmpfs 485043 1 485042 1% /dev/shm
tmpfs 485043 594 484449 1% /run
tmpfs 485043 16 485027 1% /sys/fs/cgroup
/dev/sda1 2621440 248965 2372475 10% /
/dev/loop0 103584 422 103162 1% /tmp
tmpfs 485043 1 485042 1% /run/user/0
[root@host /]# 

As you can see above, command df -i will show you how many inodes your partitions have, how many of them are used, and where they are mounted.

Why are there so many Inodes?

From our experience, the most significant usage of inodes is in old emails or in session files that are not getting removed correctly or held back. We will show how to get a breakdown of directories where most inodes are being used and how to check inode usage in specific directories.

For example, if we want to list the five directories that use the most inodes in our /var directory, we would use a command like this.

[root@host var]# du --inode | sort -rh | head -5
13534 .
7612 ./lib
6414 ./lib/yum
5721 ./lib/yum/yumdb
4599 ./cpanel

The du --inode command will list the usage of inodes. We then pipe that output into a sort -rh command, which will sort our directories from the one that uses the most inodes, to the one that uses the least number of inodes. We then pipe that info into the head -5 command will show us only the top five directories. If we want to list more than five, we can use another number, like -10, for example. This will give us the top ten directories with the most inodes.

Note:
The du command totals up space of files that exist in the filesystem, while df shows the number of blocks available in the filesystem.

After this, we can move inside a directory that uses the most inodes and rerun the same command until we get to the directory we need. For example, let’s say that we have a significant number of emails that are several years old. We can run this command in our mail directory.

[root@host mail]# du --inode | sort -rh | head -10
22 .
4 ./.Trash
4 ./.Sent
4 ./.Junk
4 ./.Drafts
1 ./.Trash/tmp
1 ./.Trash/new
1 ./.Trash/cur
1 ./tmp

We should point out that we are running this command on a newer server where few emails exist. Here, we see four inodes being used in the .Trash directory., If we want to free up those inodes, we can cd into the .Trash directory and remove those files using the rm command. Many useful bash one-liners can be used to find and remove any files that are using up inodes. A bash one-liner is a single command composed of several parts that separate, sort, or manipulate our findings in a more manageable way.

Locate Inode Number

If we only want to see the inode or index number that a specific directory/file has assigned to it, we can use the ls command with the -i flag. For example:

[root@host]# ls -i mail
136660 caloriescounter.tk 137451 cur 137687 mailbox_format.cpanel 137494 new 137589 tmp
 or using the -il flags to get a complete listing.
[root@host]# ls -il
total 692
           12 lrwxrwxrwx 1 root root 7 Apr 23 2020 bin -> usr/bin
         8193 drwxr-xr-x 2 root root 4096 Apr 23 2020 boot
         1025 drwxr-xr-x 7 root root 2760 Nov 17 14:36 dev
        16385 drwxr-xr-x 129 root root 12288 Nov 17 14:36 etc
        16386 drwxr-xr-x 3 root root 4096 Sep 17 13:49 home
         1559 lrwxrwxrwx 1 root root 7 Apr 23 2020 lib -> usr/lib
         1560 lrwxrwxrwx 1 root root 9 Apr 23 2020 lib32 -> usr/lib32
         1561 lrwxrwxrwx 1 root root 9 Apr 23 2020 lib64 -> usr/lib64
           11 drwx------ 2 root root 16384 Apr 10 2019 lost+found
        24577 drwxr-xr-x 2 root root 4096 Apr 23 2020 media
        24578 drwxr-xr-x 7 root root 4096 Oct 9 17:29 mnt
        32769 drwxr-xr-x 3 root root 4096 Oct 28 12:25 opt
            1 dr-xr-xr-x 116 root root 0 Nov 17 14:36 proc
        40961 drwx------ 7 root root 4096 Oct 28 12:25 root
         2065 drwxr-xr-x 8 root root 180 Nov 17 14:36 run
         1565 lrwxrwxrwx 1 root root 8 Apr 23 2020 sbin -> usr/sbin
        49153 drwxr-xr-x 2 root root 4096 Apr 10 2020 snap
        49154 drwxr-xr-x 2 root root 4096 Apr 23 2020 srv
            1 dr-xr-xr-x 11 root root 0 Nov 17 14:36 sys
        57346 drwxrwxrwt 4 root root 4096 Nov 18 13:36 tmp
        65537 drwxr-xr-x 14 root root 4096 Apr 23 2020 usr
        40963 drwxr-xr-x 13 root root 4096 Apr 23 2020 var
[root@host]# 

Here we used the ls -i command on our mail directory, and as you can see, we were provided an index number assigned to each directory inside the mail directory. 

Remove Inodes

We can also remove a file using its inode number, which will free up the inode number assigned to the file. This is the command that accomplished this.

[root@host]# find / -inum 137589 -exec -rm -i {}\;

The find command tells the system to locate an object (The / in this command stands for anything inside the root of the folder). The -inum flag searches specifically by inode/index number. The number after it is just an inode number that we found using the ls -i command. The -exec and -rm -i command will begin the file removal process. It will also prompt us first if we are sure we want to remove it. We can use simple use ‘y’ for yes or ‘n’ for no. In the example above, we removed the entire tmp directory from the mail directory of this account. 

If we get to the point where our system errors out due to the number of inodes, we can use a bash command like this to address this.

[root@host]# find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n

or

[root@host]# find . -maxdepth 1 -type d | grep -v '^\.$'  | xargs -n 1 -i{} find {} -xdev -type f | cut -d "/" -f 2 | uniq -c | sort -n

Conclusion

In summarize. An inode in Linux is a data structure that stores metadata about individual files and folders. Every folder and file in Linux has an inode assigned to it upon creation. We can free space on our HDD, but that does not mean that we have not used up all our inodes. We learned how to check how many inodes are used, and how to get the exact inode number of a particular file. This will make locating and deleting inodes much easier. We hope this information benefits you in going forwards!

Join Us!

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our technical support staff is always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.

We are available, via our ticketing systems at support@liquidweb.com, by phone (at 800-580-4986) or via a LiveChat or whatever method you prefer. We work hard for you so you can relax.

Avatar for Dean Conally

About the Author: Dean Conally

I am a Linux enthusiast and console gamer, dog lover, and amateur photographer. I've been working at Liquid Web for a bit less than two years. Always looking for knowledge to expand my expertise, thus tackling new technologies and solutions one day at a time.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article