Files written to Linux filesystems are assigned an inode. These unique IDs are used by the filesystem’s database in order to keep track of files. In this tutorial, you are going to learn how to view the inode number assigned to a file or directory.
There are two commands that can be used to view a file or directory’s inode, and they are
stat. Both of which are covered below.
ls command is useful for discovering the inode number for a list of files in a directory, while the
state command is better suited for single files or directories.
Using ls command
The simplist method of viewing the assigned inode of files on a Linux filesystem is to use the
ls command. When used with the
-i flag the results for each file contains the file’s inode number.
276944 drwxr-xr-x 16 www-data www-data 4096 Jun 4 2019 html 405570 drwxr-xr-x 5 www-data www-data 4096 Jun 10 21:48 wordpress
In the example above two directories are returned by the
ls command. The first column of the returned listing is the assigned inode.
- the html directory was assigned inode 276944
- the wordpress directory was assigned inode 405570
Using stat command
Another method of viewing a file’s inode is to use the
stat command. This method is generally used against a single file, while the
ls command is used against a list of files.
The example will
stat the html directory seen above.
File: ./html Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 801h/2049d Inode: 276944 Links: 16 Access: (0755/drwxr-xr-x) Uid: ( 33/www-data) Gid: (33/www-data) Access: 2019-12-06 13:33:13.194964943 +0000 Modify: 2019-06-04 01:47:16.000000000 +0000 Change: 2019-12-06 13:33:05.246318669 +0000
As you can see from the output of
state the inode value returned is the same as the one from the
ls command: 276944.