Python Directory Size Analysis with du & ncdu
While `df` measures total filesystem partitions, `du` (disk usage) measures the space consumed by specific directories and files. Combining `du -sh * | sort -h` quickly pinpoints which directories are consuming your storage.
"If `df` tells you your house is 90% full of clutter, `du` walks into every bedroom, closet, and drawer to tell you the exact weight of each box."
Deep Dive: How It Works
Summary Flag (-s): `du -sh /var/log` reports the single total aggregated size of a directory.
Depth Control (-d / --max-depth): `du -h -d 1 /var` lists immediate subdirectories without recursing infinitely into deep trees.
Apparent Size vs Disk Allocation: `--apparent-size` measures raw bytes; default `du` measures actual 4KB disk blocks allocated.
Syntax Blueprint
du -sh /var/log # Total summary of directory du -h -d 1 /var # Top-level directory breakdown du -ah /tmp | sort -rh | head -n 10 # Top 10 largest items
Use du -sh for quick summaries and du -h -d 1 for drill-down analysis.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Running plain `du /` without `-s` or `-d` on a production server.Why it happens: du recursively scans and prints millions of individual files, flooding stdout and thrashing disk IO.
How to fix: Always provide `-s` or `-d 1` to constrain output depth.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Measure Summary Size of a Folder
Create `/tmp/test_size_dir` and generate a test file inside.
Run `du -sh /tmp/test_size_dir` to print the summary.
Clean up the folder.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.