When Linux stores a file, it does not use the file name to find it on the disk; instead, it relies on a hidden data structure called an inode. So, what is an inode in linux exactly? Think of it as the file’s ID card—it holds all the metadata about the file except its name and actual data.
Every file and directory on a Linux system has one inode. Without inodes, your system wouldn’t know where the file’s data blocks are located, who owns the file, or when it was last modified. They are the backbone of the Linux filesystem.
In this guide, you’ll learn what an inode is, how it works, and why it matters for everyday Linux use. We’ll cover practical commands, common errors, and even how to check your inode usage. Let’s get started.
What Is An Inode In Linux
An inode (index node) is a data structure on a filesystem that stores metadata about a file or directory. It does not store the file name or the file content. Instead, it keeps everything else: permissions, timestamps, ownership, and pointers to the data blocks on the disk.
When you create a file, the filesystem assigns it a unique inode number. This number is the file’s true identity. The file name is just a human-readable label in a directory that points to that inode number.
Here’s a quick breakdown of what an inode contains:
- File type (regular file, directory, symlink, etc.)
- Permissions (read, write, execute for owner, group, others)
- Owner and group IDs
- File size in bytes
- Timestamps: access time (atime), modification time (mtime), and change time (ctime)
- Link count (number of hard links pointing to this inode)
- Pointers to the disk blocks where the file’s data is stored
Notice what’s missing: the file name. That’s stored in the directory entry, not the inode. This separation is why you can rename a file without changing its inode number.
How Inodes Work In Practice
When you run a command like ls -l, Linux reads the directory entry to get the file name, then looks up the inode to fetch the metadata. The inode number acts as a key to a table on the disk.
Let’s see this in action. Open a terminal and type:
ls -li
The -i flag shows the inode number. You’ll see a column of numbers on the left. Each number is unique for that filesystem.
Here’s an example output:
1234567 -rw-r--r-- 1 user user 1024 Mar 10 10:00 file.txt
The number 1234567 is the inode number. The rest is metadata pulled from that inode.
Now, try renaming the file:
mv file.txt newname.txt
ls -li
The inode number stays the same. Only the directory entry changed. This is why moving or renaming a file is so fast—it doesn’t touch the inode or the data.
How The Filesystem Uses Inodes
The filesystem maintains an inode table, which is a fixed-size array of inodes. When you format a partition, the number of inodes is set. Each inode consumes a small amount of space (typically 128 or 256 bytes).
When you create a file, the filesystem allocates a free inode from the table. When you delete a file, the inode is marked as free, and the data blocks are released.
This design has a practical limit: you can run out of inodes even if you have free disk space. This happens when you create millions of tiny files. Each file consumes one inode, and the inode table is finite.
Inode Limits And Filesystem Types
Different filesystems handle inodes differently. Here are common ones:
- ext4: Default on many Linux distros. Inode size is 256 bytes. You can set the number of inodes during formatting with
mkfs.ext4 -N. - XFS: Uses dynamic inode allocation. Inodes are created on demand, so you rarely run out.
- Btrfs: Also dynamic. Inodes are allocated as needed, but the filesystem has other limits.
- NTFS: Not native to Linux, but can be read/written. Uses a different structure called MFT (Master File Table).
To check your filesystem type and inode usage, use:
df -i
This shows the total inodes, used inodes, and available inodes for each mounted filesystem. If the “IUse%” column hits 100%, you can’t create new files until you delete some.
Common Inode-Related Commands
You’ll use these commands to inspect and manage inodes. Practice them on your system.
View Inode Numbers
As shown earlier, ls -li lists inode numbers. For a single file:
stat file.txt
The stat command gives detailed inode information, including:
- File size
- Blocks allocated
- Inode number
- Links count
- All three timestamps
Find A File By Inode
If you know the inode number but not the file name, use find:
find /path -inum 1234567
This searches the filesystem for the file with that inode. It’s useful when a file name is corrupted or you have a broken symlink.
Check Inode Usage
Use df -i to see inode usage per filesystem. For a directory, use:
ls -li /some/directory | wc -l
But that counts directory entries, not inodes. For accurate inode count in a directory tree:
find /some/directory -type f | wc -l
This counts files, each of which uses one inode.
Delete A File By Inode
Rarely needed, but possible. If a file has a weird name (e.g., starting with a dash), you can delete it using its inode:
find . -inum 1234567 -delete
Be careful—this deletes the file permanently.
Inodes And Hard Links
Hard links are a direct consequence of how inodes work. When you create a hard link, you create another directory entry pointing to the same inode. Both file names share the same inode number and data blocks.
Here’s how to create a hard link:
ln original.txt link.txt
ls -li
You’ll see both files have the same inode number. The link count in the inode increases by one.
If you delete one of the names, the data remains accessible through the other name. The inode is only freed when the link count drops to zero.
This is different from a symbolic link (symlink), which is a special file that points to another file name. Symlinks have their own inode and don’t affect the link count of the target.
Hard Link Limitations
- Cannot cross filesystems (inode numbers are unique only within one filesystem)
- Cannot link to directories (except for the special
.and..entries) - If you delete the original, the hard link still works
Inode Errors And How To Fix Them
Two common errors relate to inodes:
No Space Left On Device (But Disk Has Space)
You try to create a file, but get “No space left on device” even though df -h shows free space. Check df -i—you probably ran out of inodes.
Fix: Delete unnecessary files, especially small ones. Use find /path -type f -size 0 -delete to remove zero-byte files. Or move files to another filesystem with more inodes.
Inode Corruption
Rare, but possible due to power failure or disk errors. Symptoms include missing files, strange behavior, or filesystem check errors.
Fix: Run fsck on the unmounted filesystem. For example:
sudo fsck /dev/sda1
This checks and repairs inode inconsistencies. Always backup important data first.
How To Increase Inode Count
You cannot increase inodes on an existing filesystem without reformatting. The inode table is created during formatting.
If you’re setting up a new filesystem and expect many small files, increase the inode count:
sudo mkfs.ext4 -N 1000000 /dev/sdb1
This creates 1 million inodes. Adjust the number based on your needs. Alternatively, use a filesystem with dynamic inodes like XFS or Btrfs.
For existing systems, you can:
- Move small files to a filesystem with more inodes
- Use tar archives to store many small files in one large file
- Switch to XFS or Btrfs on new partitions
Inodes And Performance
Inodes affect performance in two ways:
- Directory lookups: When you access a file, the kernel reads the directory entry, then the inode. If the inode table is fragmented, this takes longer.
- Inode cache: Linux caches inodes in memory. If you have millions of files, the cache can grow large, consuming RAM.
For high-performance systems, keep directories small (few thousand files) and use filesystems optimized for your workload. For example, XFS handles large directories better than ext4.
Real-World Example: Debugging Inode Exhaustion
Imagine you run a mail server. One day, users can’t receive emails. You check disk space—it’s fine. But df -i shows 100% inode usage.
You find the culprit:
find /var/spool/mail -type f | wc -l
Millions of tiny mail files. You delete old emails:
find /var/spool/mail -mtime +30 -delete
This frees inodes. To prevent recurrence, set up log rotation or move to a filesystem with more inodes.
Inodes And Backups
When backing up files, inode numbers are not preserved across filesystems. Backup tools like rsync or tar store file metadata but not the inode number itself.
This is fine because the inode number is only meaningful on the original filesystem. After restore, files get new inode numbers.
However, hard links are tricky. If you have hard-linked files, backup tools must preserve the link structure. rsync -H and tar --hard-links handle this correctly.
Frequently Asked Questions
What Happens If I Run Out Of Inodes?
You cannot create new files or directories, even if disk space is available. You must delete files to free inodes. Check with df -i.
Can Two Files Have The Same Inode Number?
Yes, if they are hard links to the same file. On different filesystems, inode numbers can repeat, but they are unique within a single filesystem.
How Do I Find The Inode Of A File?
Use ls -li filename or stat filename. The inode number is shown in the output.
Does Moving A File Change Its Inode?
No, moving within the same filesystem preserves the inode. Moving to a different filesystem creates a new file with a new inode.
What Is The Difference Between An Inode And A File Descriptor?
An inode is a disk-level data structure. A file descriptor is a kernel handle used by running processes. Each open file gets a file descriptor that points to the inode.
Summary
Understanding inodes helps you manage filesystems, troubleshoot errors, and optimize performance. Every file you create consumes one inode, so keep an eye on usage with df -i.
Now you know what an inode in linux is: the hidden metadata structure that makes file management possible. Practice the commands, watch for inode exhaustion, and you’ll be a more confident Linux user.