Lab 15: Disk Usage — df and du

Objective

Monitor disk space with df (disk free) and du (disk usage): identify full filesystems, find space hogs, build disk monitoring scripts. A full disk brings down production servers — this is operational survival knowledge.

Time: 25 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: df — Filesystem Space Overview

df -h

📸 Verified Output:

Filesystem                         Size  Used Avail Use% Mounted on
overlay                             98G   33G   60G  36% /
tmpfs                               64M     0   64M   0% /dev
shm                                 64M     0   64M   0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv   98G   33G   60G  36% /etc/hosts
tmpfs                               61G     0   61G   0% /proc/acpi

Columns: Filesystem, Size, Used, Avail, Use%, Mounted on

💡 -h = human-readable (G/M/K instead of blocks). Watch for filesystems at 80%+ use — at 100% writes fail, applications crash, and logs stop recording. Set up alerts at 80%.


Step 2: df with Inode Information

📸 Verified Output:

💡 Inodes store file metadata. You can run out of inodes even with disk space available — this happens when you have millions of tiny files (e.g., npm cache, mail spools). A server with 0% inode free crashes exactly like one with 0% disk space free.


Step 3: du — Directory Usage

📸 Verified Output:

📸 Verified Output:

💡 -s = summary (total for each argument, not every subdirectory). -h = human-readable. sort -rh sorts human-readable sizes in reverse (largest first).


Step 4: du with Depth Control

📸 Verified Output:


Step 5: Finding the Largest Files

📸 Verified Output:


Step 6: du with Grand Total

📸 Verified Output:

💡 -c adds a grand total line at the end. Use du -ch /var/log/*.log to see total log space at a glance before deciding how much to rotate/compress.


Step 7: Monitoring Disk Usage Over Time

📸 Verified Output:


Step 8: Capstone — Full Log Cleanup Simulation

📸 Verified Output:


Summary

Command
Purpose

df -h

Filesystem free space

df -i

Inode usage

du -sh dir

Directory total size

du -sh dir/* | sort -rh

Sort directories by size

du -ch files | tail -1

Grand total

du --max-depth=N

Limit depth

find -printf '%s %p\n' | sort -rn

Find largest files

Last updated