7.5 Monitoring Disk Usage: df, du & tune2fs (104.2)
Key Takeaways
- The `df` (disk free) utility inspects filesystem superblock metadata to report storage capacity, mount points, and inode exhaustion (`df -i`).
- The `du` (disk usage) utility recursively traverses directory trees to calculate space consumed by individual files and directories, supporting summary mode (`-s`), human units (`-h`), and max depth (`-d`).
- Discrepancies where `df` reports a disk as 100% full while `du` shows low usage occur when deleted files remain held open by running processes (diagnosed with `lsof +L1` or `lsof | grep deleted`).
- `tune2fs` allows non-destructive inspection (`-l`) and tuning of ext2/ext3/ext4 parameters including volume labels (`-L`), UUIDs (`-U`), reserved root blocks (`-m`), max mount counts (`-c`), and check intervals (`-i`).
- Filesystem labels are managed across filesystem types using `e2label` / `tune2fs -L` (ext2/3/4), `xfs_admin -L` (XFS), and `fatlabel` (FAT32).
7.5 Monitoring Disk Usage: df, du & tune2fs
Quick Summary: Linux administrators monitor storage capacity and health using
df(to evaluate overall filesystem capacity and inode consumption) anddu(to track directory tree disk usage). For ext2/ext3/ext4 filesystems, thetune2fsutility enables administrators to inspect superblock parameters (-l) and modify settings—including volume labels (-L), UUIDs (-U), reserved root block percentages (-m), and error behaviors (-e)—on live systems without reformatting.
1. Filesystem Capacity Monitoring with df
The df (disk free) utility queries the superblock and kernel Virtual File System (VFS) statistics of all currently mounted filesystems to report available, used, and total storage capacity.
Command Syntax:
df [options] [file|device...]
Essential df Command Flags Reference
| Flag | Long Option | Operational Description & Exam Context |
|---|---|---|
-h | --human-readable | Formats sizes in human-readable powers of 1024 (KiB, MiB, GiB, TiB). |
-H | --si | Formats sizes in powers of 1000 (KB, MB, GB, TB), matching hard drive vendor specifications. |
-T | --print-type | Prints the filesystem type column (ext4, xfs, vfat, tmpfs). |
-t <type> | --type=<type> | Includes only filesystems matching <type> (e.g., df -t ext4). |
-x <type> | --exclude-type | Excludes filesystems of type <type> (e.g., df -x tmpfs -x devtmpfs). |
-i | --inodes | Reports inode usage statistics (Inodes, IUsed, IFree, IUse%) instead of block storage space. |
-a | --all | Includes dummy, virtual, and 0-block pseudo filesystems (proc, sysfs, cgroup). |
-k | (none) | Displays sizes in 1-KiB blocks (standard POSIX default behavior). |
-m | (none) | Displays sizes in 1-MiB blocks. |
-l | --local | Restricts reporting strictly to local filesystems, ignoring network mounts (NFS, CIFS). |
# Display human-readable filesystem usage with filesystem types
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 40G 18G 20G 48% /
/dev/sda1 vfat 512M 32M 480M 7% /boot/efi
/dev/sdb1 xfs 200G 45G 156G 23% /srv/data
tmpfs tmpfs 7.8G 0 7.8G 0% /dev/shm
2. Inode Exhaustion & The df -i Metric
Every file, directory, and symbolic link created on an ext2/ext3/ext4 filesystem requires an inode to store its metadata. When an ext filesystem is created with mkfs.ext4, a fixed number of inodes is generated in the inode table.
The "No Space Left on Device" Inode Trap
If a service (such as an unmonitored mail server or session cache) creates millions of 0-byte or tiny files, it can consume 100% of available inodes while using almost none of the actual disk byte capacity. When this happens, any attempt to create a new file fails with No space left on device even though df -h shows gigabytes of free disk space!
# Diagnose inode exhaustion with df -i
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 2621440 2621440 0 100% /var/spool/mail
/dev/sdb1 13107200 421090 12686110 4% /srv/data
💡 LPIC-1 Exam Fill-in-the-Blank Alert: What
dfcommand-line option displays filesystem inode usage statistics rather than block capacity? Answer:-i(or--inodes)
3. Directory & File Space Analysis with du
While df operates at the filesystem level, du (disk usage) measures the actual disk space consumed by directories and files by traversing the directory tree.
Command Syntax:
du [options] [directory|file...]
Essential du Flags Reference
| Flag | Long Option | Detailed Purpose & Exam Context |
|---|---|---|
-h | --human-readable | Displays sizes in human units (K, M, G). |
-s | --summarize | Displays only a total size for each specified argument, suppressing subdirectory breakdown. |
-a | --all | Displays counts for all individual files, not just directories. |
-c | --total | Produces a grand total line at the bottom of the output. |
-d <N> | --max-depth=<N> | Limits directory recursion to <N> levels deep (e.g., -d 1 shows immediate subdirectories only). |
-x | --one-file-system | Skips directories residing on different filesystems / mount points during traversal. |
--exclude=<pat> | Exclude Pattern | Skips files matching shell pattern <pat> (e.g. --exclude="*.log"). |
-b | --bytes | Displays sizes in exact raw bytes. |
Practical du Commands for System Administration
# 1. Summarize total size of /var/log in human units
$ du -sh /var/log
4.2G /var/log
# 2. Inspect space used by immediate subdirectories under /var (depth 1)
$ du -h --max-depth=1 /var
1.2G /var/cache
4.2G /var/log
850M /var/lib
6.3G /var
# 3. Identify the 5 largest directory consumers on the root filesystem (staying on one filesystem)
# du -xh / | sort -rh | head -n 5
The df vs. du Discrepancy (Deleted Open Files Trap)
A classic Linux administration dilemma occurs when df reports a partition is 100% full, but du -sh / calculates only 30% usage.
Root Cause: When a large file (e.g., a 50 GB log file) is deleted with rm while a running process (e.g., rsyslogd or Java) still holds an open file descriptor to it, the directory link is removed, but the inode and data blocks remain allocated on disk until the process terminates or closes the descriptor.
# Locate deleted files that are still held open in memory by running processes
# lsof +L1
# Or filter lsof for deleted entries:
# lsof | grep deleted
java 4810 root 3u REG 8,2 52428800000 189204 /var/log/app.log (deleted)
# Resolution: Restart the holding service to release the disk blocks
# systemctl restart app-service
4. Superblock Inspection & Parameter Tuning with tune2fs
The tune2fs utility allows administrators to inspect and adjust tunable filesystem parameters on ext2, ext3, and ext4 filesystems without reformatting or destroying data.
Command Syntax:
tune2fs [options] <device>
Essential tune2fs Command Options Reference
| Option Flag | Detailed Purpose & Exam Context |
|---|---|
-l <device> | List superblock contents. Dumps all parameters stored in the ext superblock (block count, inode count, UUID, volume name, mount count, check interval, reserved blocks, filesystem state, features). |
-L <label> | Set volume label. Sets or updates the filesystem volume label (up to 16 characters). |
-U <UUID> | Set UUID. Sets a specific UUID or generates a new one with tune2fs -U random <device>. |
-m <percent> | Adjust reserved root space percentage. Adjusts percentage of reserved superuser blocks (e.g. tune2fs -m 1 /dev/sda1). |
-r <blocks> | Adjusts the reserved space by an exact numeric block count. |
-c <max> | Max mount count before fsck. Sets the maximum number of times the filesystem can be mounted before triggering an automatic e2fsck at boot (-c 0 or -c -1 disables this check). |
-i <interval> | Time interval between checks. Sets maximum time between filesystem checks (e.g., -i 180d for 180 days, -i 0 to disable). |
-C <count> | Sets the current filesystem mount counter. |
-e <action> | Set kernel error behavior. Controls kernel reaction when filesystem errors are detected: continue (normal execution), remount-ro (remount read-only to protect data, default), or panic (force kernel panic and halt). |
-j | Add ext3 journal. Adds an ext3 journal to an existing ext2 filesystem, non-destructively upgrading it to ext3. |
-o [^]<opts> | Sets or clears (^) default mount options embedded in the superblock (e.g., acl, user_xattr). |
Practical Superblock Inspection with tune2fs -l
# Inspect superblock details of an ext4 filesystem
# tune2fs -l /dev/sda2
tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name: ROOT_VOL
Filesystem UUID: a28b12f4-7e11-42b8-a6d1-817290123456
Filesystem magic number: 0xEF53
Filesystem state: clean
Errors behavior: Remount read-only
Filesystem OS type: Linux
Inode count: 2621440
Block count: 10485760
Reserved block count: 104857 (1.00%)
Free blocks: 6120491
Free inodes: 2410882
Block size: 4096
Mount count: 14
Maximum mount count: 30
Last checked: Mon Aug 25 14:20:00 2026
Check interval: 15552000 (180 days)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit
5. Filesystem Volume Label Management Utilities
Volume labels allow disks to be identified and mounted persistently via LABEL=name in /etc/fstab rather than relying on shifting /dev/sd* device node names.
Label Management Tools Across Filesystem Types
| Filesystem Type | View Current Label | Change / Set New Label | Maximum Label Length |
|---|---|---|---|
| ext2 / ext3 / ext4 | e2label /dev/sda1 | e2label /dev/sda1 NEW_LABEL<br/>tune2fs -L NEW_LABEL /dev/sda1 | 16 Characters |
| XFS | xfs_admin -l /dev/sdb1 | xfs_admin -L NEW_LABEL /dev/sdb1 (Must be unmounted) | 12 Characters |
| VFAT / FAT32 | fatlabel /dev/sdc1 | fatlabel /dev/sdc1 NEW_LABEL<br/>dosfslabel /dev/sdc1 NEW_LABEL | 11 Characters |
| Btrfs | btrfs filesystem label /mnt | btrfs filesystem label /mnt NEW_LABEL | 255 Characters |
# Set and view volume labels using e2label
# e2label /dev/sda2 SYS_ROOT
# e2label /dev/sda2
SYS_ROOT
A web server's logging partition is unable to accept new log files, returning the error No space left on device. However, executing df -h shows 80 GiB of free space available. Which command will best help diagnose if the filesystem has exhausted its available inode table entries?
An administrator needs to inspect the current maximum mount count, check interval, and reserved block percentage stored in the superblock of an ext4 filesystem on /dev/sdb1. Which command should be executed?
After deleting a 30 GB video recording file from /var/media/ using rm, the administrator notices that df -h still indicates /var is 100% full, whereas du -sh /var shows 30 GB less usage. What is the most likely cause of this discrepancy?