howtouselinux

2 Ways to Get File Inode Number in Linux

Table of Contents

In computing, an inode (index node) is a data structure used to represent a file or directory. Each inode stores the attributes and disk location of the file’s data. Filesystems that support extended attributes use additional entries in the inode structure to store these attributes.

There are two ways to get the inode number for a file. The first way is to use the ls command with the -li option. This will show the inode number for each file listed.

The second way is to use the stat command. This will show all of the information about a file, including the inode number.

In this blog post, we will show you how to do both!

understanding inode in Linux

Inode numbers are used internally by the Linux kernel to keep track of files. Each inode number is unique and is assigned when the file is created. The inode number can be used to identify a file even if the name is changed or removed.

There are a few instances where you may need the inode number for a file. For example, if you want to delete a file but its name has a special character in it, you will need to know its inode number. You can use the ls command with the -i option to get this information. Then you can use the find command to delete it.

Get file inode number with ls -li in Linux

The best way to get the inode number of a file in Linux is using the command ls -li filename. This command will give you a lot of information about the file. The inode number will be displayed on the first column of the output.

For example, if we have a file named test.txt, we would type: ls -li test.txt

which would give us the output:

1200 -rw-r–r– 1 root root 2049 Apr 18 2034 test.txt

The inode number for this file is “1200”.

The ls command is used to display the contents of a directory. By default, it will show the name for each file and directory under the directory. You can use various options with the ls command to change the output.

For example, if we want to see the inode numbers for all of the files in our current directory, we would type: ls -li

This would give us the output:

8398668 drwxrwxr-x. 14 postgres postgres    152 Sep 12 09:21 test
16800775 drwx——. 2 root   root       6 Sep 16 07:12 testdir
25173208 drwx——. 2 root   root       6 Sep 14 23:58 testdir2

As you can see, the inode number is displayed on the left side of the output.

Check file inode number with stat command in Linux

We can use stat command to get the inode number of a file in Linux. This command will give you a lot of information about the file such as file size, file permission, inode number etc. 

Inode: 1200 Links: 13

The number after the word “Inode:” is the inode number. In the example above, it is “1200”.

The stat command has a number of options, including the -c option. This option allows you to output the inode number as a character.The stat command with -c option allows you to use a particular or custom format instead of the default.

we can use stat -c “%i” filename to get the inode number directly.

For example:
stat -c “%i” test.txt
1200

For example, you can use the following command to check the file type in Linux.

stat -c “%F” test.txt 
regular file

The stat command is used to get information about files and directories, including the inode number. You can use it to get information such as the size of the file, when it was last modified, who owns it, and more.

Tips to get file inode number in Linux

Please note that getting the inode number of a directory will give you the inode number of the directory itself, not the files inside it. If you want to get the inode number of a file inside a directory, you will need to specify the path to the file. For example, if we have a directory named “test” with a file named “test.txt” inside it, we would type:

ls -l test/test.txt

which would give us the output:

1200 -rw-r–r– 2 root root 4096 Apr 18 2034 test/test.txt

The inode number for this file is “1200”.

If you want to get the inode number of a directory, you can use the -d option with ls:

ls -ld test

which would give us the output:

4096 drwxr-xr-x 3 root root 4096 Apr 18 2034 test/

The inode number for this directory is “4096”.

As you can see, getting the inode number of a file or directory in Linux is a very simple process. Thanks for reading and have a great day!

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.