df and du Deep Dive

Concept

  • df reports filesystem-level allocation (what the filesystem thinks is used/free).
  • du walks the directory tree and sums the size of files it can see.

They often disagree. Understanding why is a core troubleshooting skill.

Why it matters

A large gap between df and du usually means one of:

  • Deleted files that are still held open by a process
  • Mount points hiding data
  • Permission or namespace differences
  • Different filesystems / bind mounts

Mental Model

df  = “How full is the filesystem?”
du  = “How much space do the visible files under this path consume?”

When df shows high usage but du shows much less → look for deleted-but-open files or data hidden under mount points.

Key Commands

# Filesystem usage
df -hT
df -i
 
# Directory usage (one level)
du -xhd1 /var 2>/dev/null | sort -hr | head -20
 
# Summary of a tree
du -sh /var/log
 
# Exclude other filesystems (-x is important)
du -xhd1 / 2>/dev/null | sort -hr
 
# Find large files
find /var -xdev -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -hr | head

Common Discrepancy Causes

SituationWhat you seeHow to confirm
Deleted file still opendf high, du lowerlsof +L1 or `lsof
Data under a mount pointdu on parent misses the datafindmnt, mount, check order
Permission denieddu under-reportsRun as root, check errors
Different filesystemdu without -x crosses mountsAlways use -x or -xdev
Sparse filesdu and ls can differls -ls, du --apparent-size

Investigation Tips

  • Always use du -x (or find -xdev) when investigating a single filesystem.
  • When df and du disagree significantly, run lsof +L1 first.
  • For interactive exploration, ncdu -x /path is excellent if available.

Personal Lessons Learned