Every file on a Linux system has a unique inode number that identifies its data blocks. If you need to manage files, troubleshoot disk issues, or work with hard links, knowing how to find inode number of a file in linux is a essential skill. This guide walks you through every method, from simple commands to advanced tricks, so you can get the inode number fast.
Inodes store metadata about a file—like permissions, ownership, timestamps, and location on disk—but not the file name itself. The inode number is a numeric identifier that the filesystem uses internally. You can see it with a few terminal commands, and we will cover all of them here.
What Is An Inode Number In Linux
An inode (index node) is a data structure on a filesystem that holds information about a file or directory. Each file has one inode, and that inode has a unique number within its filesystem. The inode number points to the file’s data blocks on the disk.
When you create a file, the system assigns an inode number. This number stays the same even if you rename or move the file within the same filesystem. Hard links share the same inode number because they point to the same data.
Knowing the inode number helps with tasks like finding duplicate files, checking filesystem health, or deleting files with problematic names. It is a fundamental concept for Linux administrators and power users.
How To Find Inode Number Of A File In Linux
The most direct way to find the inode number is using the ls command with the -i flag. Open your terminal and type:
ls -i filename
This prints the inode number in the first column, followed by the file name. For example, ls -i report.txt might show 1234567 report.txt. That number is the inode.
If you want to see inodes for all files in a directory, run:
ls -ia
The -a flag shows hidden files too. The output lists inode numbers for every item, including . and ...
For more detail, combine -i with -l (long format):
ls -li
This shows permissions, owner, size, and the inode number in the first column. It is the most common way to check inodes while browsing files.
Using Stat Command To Get Inode Number
The stat command gives comprehensive metadata about a file, including the inode number. Type:
stat filename
Look for the line that says “Inode:” followed by a number. For example:
File: report.txt
Size: 2048 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 1234567 Links: 1
The stat command also shows device, links, access time, and more. It is perfect when you need full details along with the inode.
You can format the output to show only the inode number using stat -c %i filename. The %i format specifier prints the inode number alone. This is useful for scripting.
Using Find Command To Locate Files By Inode
Sometimes you have an inode number and need to find the file name. The find command can search by inode. Use:
find /path -inum INODE_NUMBER
Replace INODE_NUMBER with the actual number. For example, find /home -inum 1234567 lists all files with that inode in /home.
This is helpful when you have a corrupted file name or need to locate hard links. The -inum option works on any filesystem that supports inodes.
You can combine it with -type f to search only files, or -type d for directories. For instance:
find / -inum 1234567 -type f 2>/dev/null
This ignores permission errors and finds the file globally.
Using Debugfs For Advanced Inode Lookup
For ext2, ext3, or ext4 filesystems, the debugfs tool lets you inspect inodes directly. First, get the filesystem device with df:
df /path/to/file
Then run debugfs on that device:
sudo debugfs /dev/sda1
Inside the debugfs prompt, use stat to see details. Or use ls -l to list files with inodes. This is a low-level method for troubleshooting.
Exit debugfs with quit. This approach is not for daily use but is powerful for filesystem repair or forensic analysis.
Practical Examples Of Finding Inode Numbers
Let’s look at real-world scenarios. Suppose you have a file named myfile.txt in your home directory. Run:
ls -i ~/myfile.txt
Output: 654321 myfile.txt. The inode is 654321.
Now check a directory:
stat ~/Documents
Look for “Inode: 987654”. Directories also have inodes.
If you have a file with special characters in its name, use find with -inum after getting the inode from ls -i on the parent directory. For example:
ls -i /tmp/
find /tmp -inum 111222 -exec rm {} \;
This deletes a file with a weird name by referencing its inode.
Finding Inode Numbers For Multiple Files
To see inodes for all files in a directory tree, use ls -iR. The -R flag recurses into subdirectories. Output can be long, so pipe it to less:
ls -iR /var/log | less
Alternatively, use find with -printf:
find /var/log -printf "%i %p\n"
This prints inode number and path for each file. It is efficient for large directories.
Using Inode Numbers To Identify Hard Links
Hard links share the same inode number. To find all hard links to a file, use find with -inum across the filesystem:
find / -inum 1234567 2>/dev/null
This lists every path that points to the same inode. The ls -l output also shows the link count in the second column. If it is greater than 1, there are hard links.
For example, ls -li shows:
1234567 -rw-r--r-- 2 user user 1024 Jan 1 12:00 file1
1234567 -rw-r--r-- 2 user user 1024 Jan 1 12:00 file2
Both files have inode 1234567 and link count 2. They are hard links to the same data.
Common Mistakes When Finding Inode Numbers
One frequent error is confusing inode numbers with file descriptors. Inodes are filesystem-level identifiers, while file descriptors are process-level handles. They are not the same.
Another mistake is assuming inode numbers are unique across the entire system. Inodes are unique only within a single filesystem. Two different partitions can have the same inode number for different files.
Also, some users forget that ls -i shows inodes for the current directory unless a path is given. Always specify the file or directory to avoid confusion.
Finally, do not rely on inode numbers for files on network filesystems like NFS. Inodes may change or be inconsistent across clients.
Automating Inode Lookup With Scripts
You can write a simple bash script to get inode numbers for multiple files. Save this as getinode.sh:
#!/bin/bash
for file in "$@"; do
inode=$(stat -c %i "$file" 2>/dev/null)
if [ -n "$inode" ]; then
echo "$file: $inode"
else
echo "$file: not found"
fi
done
Make it executable with chmod +x getinode.sh. Run it with file names as arguments:
./getinode.sh file1.txt file2.txt
This prints each file’s inode. You can extend it to search by inode or compare duplicates.
For a one-liner to list all inodes in a directory sorted by number:
ls -i | sort -n
This sorts the output numerically by inode.
Using Inode Numbers For Filesystem Debugging
When a filesystem gets corrupted, inode numbers help identify damaged files. Tools like fsck use inodes to report errors. You can cross-reference inode numbers from error logs with find to locate the problematic file.
For example, if fsck reports “Inode 1234567 has bad blocks,” run:
find /mountpoint -inum 1234567
This shows the file path. Then you can back it up or delete it.
Similarly, debugfs can examine the inode structure directly. Use stat inside debugfs to see block pointers and permissions.
Frequently Asked Questions
What Command Shows Inode Number In Linux?
The ls -i command shows the inode number for files. stat also displays it with more details.
Can Two Files Have The Same Inode Number?
Yes, but only if they are hard links to the same data on the same filesystem. Different filesystems can have duplicate inode numbers.
How Do I Find The Inode Of A Directory?
Use ls -id directoryname or stat directoryname. Directories have inodes just like files.
What Is The Difference Between Inode And File Descriptor?
An inode is a filesystem structure that stores metadata. A file descriptor is an integer handle used by a process to access an open file.
How To Find A File By Inode Number?
Use find /path -inum INODE_NUMBER. This searches for any file with that inode in the given path.
Conclusion
Knowing how to find inode number of a file in linux is a practical skill for managing your system. You can use ls -i, stat, or find to get the number quickly. For advanced work, debugfs gives low-level access.
Remember that inodes are per-filesystem, so check your mount points if you get unexpected results. Practice with these commands on test files to build confidence. With these methods, you can handle file identification, hard links, and troubleshooting like a pro.
Now open your terminal and try it out. You will see the inode number for any file in seconds. This knowlege will save you time when dealing with complex file operations or system repairs.