The `df` command in Linux is supposed to show you the free and used disk space on your filesystem, but sometimes it displays incorrect numbers—whether too high or too low. This usually happens when the system fails to properly account for cached data, swap usage, or filesystem metadata, leading to misleading reports. If you're seeing wrong disk usage with `df`, the issue is almost always environmental or configuration-related rather than a hardware fault.
What You'll Need
- A Linux system with terminal access (Ubuntu, Fedora, Arch, etc.
- Root or sudo privileges to run some diagnostic commands
- Basic familiarity with Linux commands (especially `ls`, `mount`, `mountpoint`)
- An understanding of filesystem types (ext4, btrfs, XFS, etc.) to rule out incompatibilities
Step-by-Step
- Check for cached data affecting disk usage
Use `sync` followed by `df` to flush buffers and clear cache:
sudo sync && df -hIf the numbers change significantly, cached data may be the culprit. Test this by running `sync` multiple times in a loop until the figures stabilize. - Verify the filesystem is clean and not in a degraded state
Run:
sudo fsck -f /path/to/diskReplace `/path/to/disk` with the actual mount point (e.g., `/`). If errors are found, repair them with the appropriate tool (e.g., `e2fsck` for ext4). Always back up critical data before running `fsck`. - Check for swap usage misreported by `df`
Swap space is included in `df`'s reported used space, but some filesystems (like `swap` or overcommitted swap) may skew results. Run:
free -hCompare this with `df`'s swap numbers. If the two differ drastically, the issue is likely swap-related. - Inspect the mount options for filesystem quirks
Run:
mount | awk '{print $6}'Look for mount flags like `noatime`, `discard`, or `relatime` that might affect reporting. For example, `discard` can cause `df` to underreport space due to automatic trim operations. If you’re unsure, revert to defaults with:sudo mount -o remount /path/to/disk(Replace with your actual path; flags vary by filesystem and kernel version.) - Test with a different filesystem to isolate the issue
Create a temporary filesystem (e.g., loopback) and check its usage:
sudo mkdir /tmp/testfs && sudo mkfs.ext4 /tmp/testfs && sudo mount -o loop /path/to/loop.img /tmp/testfs && df -h /tmp/testfsIf `df` behaves normally here, the problem lies in your original filesystem’s settings or kernel module.
Common Mistakes
- Ignoring cached data: Many users assume `df` reports real space, but it often shows cached data as "available" space. Always run `sync` first.
- Overlooking swap conflicts: Swap space is included in `df`'s "total" and "used" fields, so misconfigured swap (e.g., overcommitted) can distort results.
- Using incorrect mount options: Filesystems like ZFS or Btrfs have custom reporting modes. A `discard` mount flag on ext4 can make `df` underreport space.
- Skipping `fsck` for filesystem corruption: If the filesystem is damaged (e.g., after crashes), `df` may show incorrect totals even if the disk appears healthy.
Tips
- For ZFS users: ZFS reports space differently than ext4. Use `zfs list` for accurate snapshots and pool usage instead of `df`.
- Enable debug output: Run `df -x tmpfs` to exclude tmpfs (RAM) from calculations. For ext4, add `debug` to the mount options temporarily to see internal checks.
- Monitor with `ncdu` (install via `sudo apt install ncdu`): A more granular tool that shows real-time changes in space usage by directory.
- Avoid `df`'s default `-h` flag if you need precise bytes: Use `df -B 1G` for 1GB granularity or `df` alone for raw values.
Troubleshooting
- If `df` still shows wrong values after sync and fsck Check if `df`'s output is from a custom script or wrapper. Run `ps aux | grep df` to see if a custom command is being invoked. If so, inspect its source and validate its logic.
- Kernel module issues (e.g., for LVM or encrypted disks)
Some disks (e.g., encrypted or LVM) require kernel modules to report space accurately. Verify with:
ls /sys/block/*/device/moduleIf missing, install the required module or check kernel logs (`dmesg | grep -i lvm` for LVM). - Filesystem type incompatibility Test your filesystem with another kernel or distro to see if the issue persists. If only your system shows wrong data, the problem may be a kernel-specific quirk.
- Check for disk errors with `smartctl`:
For SATA/SSD disks, run:
sudo smartctl -a /dev/sdX(Replace `sdX` with your disk, e.g., `sda`. Look for "Reallocated_Sector_Ct" or "Media_Wearout_Index" to detect failing drives.)
Frequently Asked Questions
Why does `df` show 0 free space even though I have 50GB left?
This usually means cached data is being reported as "available." Run `sync && df -h` repeatedly until the numbers stabilize. If it persists, the filesystem may be set to `discard` (common on SSDs) or have other mount flags affecting reporting.How do I fix `df` showing -1 free space?
Negative values indicate filesystem corruption. Run `fsck` on the problematic mount point:sudo fsck -f /mount/point
If `fsck` fails, boot into recovery mode or use a Live USB to fix the filesystem.