Inodes

Deep dive into inodes - the data structures that store file metadata and enable hard linking in Linux filesystems

Topic Overview

An inode (index node) is a data structure that stores metadata about files and directories in Unix-like filesystems. Every file has an associated inode that contains crucial information like permissions, timestamps, and disk block locations, but notably NOT the filename itself.

Key Concepts

  • Inode Number: Unique identifier for each file within a filesystem
  • Metadata Storage: Contains file attributes, not file content or names
  • Hard Links: Multiple filenames pointing to same inode
  • Inode Table: Filesystem structure storing all inodes
  • Inode Exhaustion: Running out of available inodes

Command Syntax

ls -i [options] [files] - Display inode numbers stat filename - Show detailed inode information df -i - Check inode usage on filesystems

Common Options

-i - Show inode numbers with ls -l - Long format (shows links count) --inode - Alternative to -i flag

Practical Examples

Example 1: View inode numbers

1
2
3
4
5
ls -li /home/user/
total 12
2621441 -rw-r--r-- 2 user user 1024 Nov 15 10:30 file1.txt
2621442 drwxr-xr-x 3 user user 4096 Nov 15 10:25 documents
2621441 -rw-r--r-- 2 user user 1024 Nov 15 10:30 hardlink.txt

Shows inode numbers in first column. Note file1.txt and hardlink.txt share same inode (2621441).

Example 2: Detailed inode information

1
2
3
4
5
stat myfile.txt
File: myfile.txt
Size: 1024      Blocks: 8       IO Block: 4096   regular file
Device: 803h/2051d  Inode: 2621441  Links: 2
Access: (0644/-rw-r--r--)  Uid: (1000/user)   Gid: (1000/user)

Displays comprehensive inode metadata including size, permissions, timestamps, and link count.

Example 3: Check inode usage

1
2
3
4
df -i
Filesystem      Inodes   IUsed   IFree IUse% Mounted on
/dev/sda1      6553600  180234 6373366    3% /
/dev/sda2       655360   45123  610237    7% /home

Shows total, used, and free inodes per filesystem.

Example 4: Find files by inode

1
2
3
find /home -inum 2621441
/home/user/file1.txt
/home/user/hardlink.txt

Locates all files sharing the same inode number.

Use Cases

  • File identification: Uniquely identify files regardless of name
  • Hard link management: Track multiple names for same file
  • Forensics: Recover files or trace file operations
  • Backup verification: Ensure file integrity across systems
  • Troubleshooting: Diagnose “No space left” errors from inode exhaustion

find -inum - Search by inode number ln - Create hard/soft links unlink - Remove directory entry (not file data) debugfs - Examine filesystem structures directly tune2fs -l - Show filesystem inode information

Tips & Troubleshooting

Common Issues

  • “No space left on device” despite free space: Check df -i for inode exhaustion
  • Cannot delete files: May need root privileges or file has special attributes
  • Broken hard links: Use find -links +1 to locate multiply-linked files

Performance Notes

  • Inode operations are generally fast (metadata only)
  • Excessive hard links can complicate file management
  • Some filesystems (FAT32) don’t use traditional inodes

Important Facts

  • Filenames are stored in directory entries, NOT inodes
  • Deleting a filename decreases link count; file deleted when count reaches 0
  • Inode numbers are unique within a filesystem, not across different filesystems
  • Moving files within same filesystem preserves inode numbers