who are you letter
Thu Jun 15

Inode in Linux - A Complete Guide With Example

If you are a Linux user, you may have heard of the term inode. But what exactly is an inode and why is it important in Linux? In this article, we will explain what an inode is, how to find and use it, and how to manage your inode usage and limit on your filesystem.

What is an inode and why is it important in Linux?

An inode is a data structure that stores information about a file or directory on a Linux filesystem. An inode contains metadata such as the file size, owner, permissions, timestamps, and links. An inode does not store the file name or the file content. Instead, it has a unique identifier called the inode number that links it to a directory entry that contains the file name.

An inode is important in Linux because it allows the system to access and manipulate files efficiently. The system uses the inode number to locate the file on the disk and perform operations such as reading, writing, deleting, or moving. The system also uses the inode metadata to check the file attributes and permissions before allowing any action.

How to find the inode number of a file or directory

One way to find the inode number of a file or directory is to use the ls command with the -i option. For example, if you want to find the inode number of a file called example.txt in your home directory, you can run:

bash
ls -i ~/example.txt

The output will show you the inode number followed by the file name:

bash
123456 example.txt

You can also use the -l option to see more details about the file:

bash
ls -li ~/example.txt

The output will show you something like this:

bash
123456 -rw-r--r-- 1 user user 1024 Jun 15 10:41 example.txt

The first column is the inode number, followed by the file type and permissions, number of links, owner, group, size, date, and name.

You can also use wildcards (*) to find the inode numbers of multiple files or directories that match a pattern. For example, if you want to find the inode numbers of all files that end with .txt in your home directory, you can run:

bash
ls -i ~/*.txt

The output will show you something like this:

bash
123456 example.txt
234567 test.txt
345678 report.txt

How to use the find command with the inode number

Another way to find a file or directory by its inode number is to use the find command with the -inum option. For example, if you want to find a file with an inode number of 123456 in your home directory, you can run:

bash
find ~ -inum 123456

The output will show you something like this:

bash
/home/user/example.txt

You can also use other options with the find command to perform actions on the file or directory that matches the inode number. For example, if you want to delete a file with an inode number of 123456 in your home directory, you can run:

bash
find ~ -inum 123456 -delete

This will delete the file without asking for confirmation.

You may wonder why you would need to use the inode number instead of the file name to find or delete a file. One reason is that some files may have special characters or spaces in their names that make them difficult to handle with the shell. Another reason is that some files may be hidden or inaccessible by their names due to permissions or corruption. In these cases, using the inode number can help you locate and manipulate the file more easily.

How to check the inode usage and limit on a filesystem

Every filesystem has a limited number of inodes that it can allocate to store files and directories. If you run out of inodes, you will not be able to create new files or directories, even if you have enough disk space. Therefore, it is important to monitor your inode usage and limit on your filesystem.

One way to check the inode usage and limit on a filesystem is to use the df command with the -i option. For example, if you want to check the inode usage and limit on your root (/) filesystem, you can run:

bash
df -i /

The output will show you something like this:

bash
Filesystem     Inodes  IUsed  IFree IUse% Mounted on
/dev/sda1     5242880 123456 5119424    3% /

The first column is the filesystem name, followed by the total number of inodes, the number of used inodes, the number of free inodes, the percentage of inode usage, and the mount point.

You can also use the -h option to see the output in a human-readable format:

bash
df -ih /

The output will show you something like this:

bash
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sda1        5.0M  123K  4.9M    3% /

You can also use wildcards (*) to check the inode usage and limit on multiple filesystems that match a pattern. For example, if you want to check the inode usage and limit on all filesystems that start with /dev/sd, you can run:

bash
df -ih /dev/sd*

The output will show you something like this:

bash
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sda1        5.0M  123K  4.9M    3% /
/dev/sda2        1.0M   12K 1020K    2% /boot
/dev/sdb1        2.0M   34K    2M    2% /home
/dev/sdc1        4.0M   56K    4M    2% /data

How to free up inodes on a filesystem

If you find that your inode usage is high or close to the limit, you may want to free up some inodes on your filesystem. One way to do that is to delete unused files or directories that are taking up inodes. You can use the find command with the -size option to find files or directories that are empty or very small and delete them. For example, if you want to find and delete all empty files or directories in your home directory, you can run:

bash
find ~ -empty -delete

This will delete all empty files or directories without asking for confirmation.

You can also use other options with the find command to specify a size range or a time range for finding and deleting files or directories. For example, if you want to find and delete all files or directories that are smaller than 1 KB and older than one month in your home directory, you can run:

bash
find ~ -size -1k -mtime +30 -delete

This will delete all files or directories that match the criteria without asking for confirmation.

Another way to free up some inodes on your filesystem is to empty the trash bin or clear the cache of your applications. These may contain temporary or deleted files that are still using inodes. You can use the rm command with the -rf option to remove these files or directories recursively and forcefully. For example, if you want to empty the trash bin in your home directory, you can run:

bash
rm -rf ~/.local/share/Trash/*

This will remove all files or directories in the trash bin without asking for confirmation.

You can also use other commands or tools to clear the cache of your applications depending on what applications you are using. For example, if you are using Firefox as your web browser, you can run:

bash
firefox --clear-cache

This will clear the cache of Firefox without opening it.

Conclusion

In this article, we have learned what an inode is, how to find and use it, and how to manage your inode usage and limit on your filesystem. We have also learned how to free up some inodes by deleting unused files or directories, emptying the trash bin, and clearing the cacheof your applications. Here are some tips to remember when working with inodes in Linux:

  • Always check your inode usage and limit on your filesystems to avoid running out of inodes and facing errors or performance issues.
  • Use the ls command with the -i option to find the inode number of a file or directory.
  • Use the find command with the -inum option to find a file or directory by its inode number and perform actions on it.
  • Use the df command with the -i option to check the inode usage and limit on a filesystem.
  • Delete unused files or directories, empty the trash bin, and clear the cache of your applications to free up some inodes on your filesystem.