Linux Command Reference ยท Concepts
Linux inodes
What an inode is in Linux: the metadata record behind every file, how names map to inodes, link counts, inode exhaustion, and commands to inspect them.
An inode (index node) is the record a Unix filesystem keeps for each file. It holds everything about the file except its name: type, owner, group, permission bits, size, timestamps, link count and the location of the data blocks. A file name is just an entry in a directory that maps text to an inode number.
This split explains many behaviors that look odd at first: why renaming is instant, why you can delete a file that a program still has open, and why a disk can be "full" with plenty of free space.
ls -i file # inode number stat file # the inode's contents df -i # inode usage per filesystem
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
What is in an inode
| Field | Notes |
|---|---|
| File type | Regular file, directory, symlink, device, FIFO, socket |
| Mode | Permission bits and setuid, setgid, sticky |
| Owner and group | Numeric UID and GID |
| Size | In bytes |
| Timestamps | Access (atime), modification (mtime), status change (ctime); some filesystems add a creation time |
| Link count | Number of directory entries that point at it |
| Block pointers | Where the data lives (extents on ext4 and XFS) |
Notice what is missing: the name. A directory is a special file whose content is a list of (name, inode number) pairs. Looking up /home/ada/notes.txt means reading each directory in turn to find the next inode.
How it explains everyday behavior
- Rename and move on one filesystem change only directory entries, so they take the same time for a 1 KB file and a 100 GB file. Moving across filesystems is a copy plus delete.
- Hard links are extra entries for one inode; the link count tracks them (Hard links vs symbolic links).
- Deleting removes an entry and lowers the link count. The inode and data are freed only when the count is 0 and no process holds the file open. That is why a deleted log can still fill a disk until the program closes it (
lsof +L1finds such files). - Inode numbers are unique per filesystem, not globally. Two files on different disks can have the same number.
- ctime is not creation time. It changes whenever the inode changes (permissions, owner, links), not only the content.
Running out of inodes
Many filesystems create a fixed number of inodes when formatted (ext4 does; XFS and Btrfs allocate dynamically). Millions of tiny files, such as mail queues or session files, can use them all while df -h still shows free space. The symptom is "No space left on device" when creating a file.
df -i /var # IUse% near 100% is the clue find /var -xdev -type f | cut -d/ -f1-4 | sort | uniq -c | sort -rn | head
On ext4 the count is chosen at mkfs time (-N or -i bytes-per-inode) and cannot be raised later without recreating the filesystem. See df and mkfs; df -i output in containers and tmpfs may show 0 or - on filesystems with dynamic inodes.
Usage notes
ls -iprints inode numbers;find -inum Nfinds the names for one;find -samefile Ffinds all hard links of F.statshows the whole inode;[ a -ef b ]tests whether two names refer to the same inode and device.- Symlinks have their own inode that stores the target path (short paths live inside the inode itself).
- Special names: every directory has
.(itself) and..(its parent), which is why a directory's link count is 2 plus its number of subdirectories on classic filesystems. Some filesystems report 1 instead. - Reusing a number: after a file is freed its inode number can be reused, so never store inode numbers as permanent identifiers.
- Backups and deduplication tools use inode numbers to detect hard links;
rsync -Hpreserves them.
Related
stat,ls,ln,df,find,lsof- Hard links vs symbolic links
- Lessons: Links, Disk Usage