du Command Examples

Disk Usage - Find What's Eating Your Storage

About du

The du (disk usage) command estimates and displays the disk space used by files and directories. It's an essential tool for system administrators to identify storage hogs, clean up disk space, and monitor directory sizes. Unlike df which shows filesystem-level usage, du provides granular, file-level disk consumption analysis. Mastering du is crucial for maintaining healthy filesystem usage and preventing disk-full emergencies.

1. Display Disk Usage in Human-Readable Format

$ du -h

Shows disk usage for the current directory and all subdirectories in human-readable format (KB, MB, GB, etc.). The -h flag converts raw byte counts into easily understandable units.

Output format: 4.5M, 128K, 2.3G instead of raw byte counts.

Use Case: Quick visual scan of directory sizes to spot large subdirectories, perfect for interactive use when you need to read the output directly.
Without -h, du displays sizes in 512-byte blocks by default (or 1K blocks with -k), which is harder to read at a glance.

2. Summary of Directory Total Only

$ du -sh /var/log

Displays only the total size of the specified directory without showing individual subdirectories. The -s flag means "summarize" and -h provides human-readable output.

Perfect for: Quick answer to "how big is this directory?" without all the subdirectory details.

Use Case: Checking total size of backup directories, log directories, or user home directories. Essential when you just need the bottom line without the breakdown.

3. Show Sizes for All Items in Directory

$ du -h --max-depth=1

Lists disk usage for the current directory and immediate subdirectories only (one level deep). This prevents deep recursion and gives you a clear first-level overview.

Depth control: --max-depth=1 shows only direct children, =2 shows children and grandchildren, etc.

Use Case: Identifying which top-level subdirectories are consuming the most space. This is often the first step in disk space troubleshooting - find the big directory, then drill down.
On some systems, you can use -d 1 as a shorter alternative to --max-depth=1

4. Sort Output by Size (Largest First)

$ du -h | sort -hr

Combines du with sort to display directories by size, largest first. The -h flag on sort enables "human numeric sort" that correctly orders units (KB, MB, GB).

Sort flags: -h (human-readable), -r (reverse/descending order)

Use Case: Quickly finding the biggest disk space consumers. The largest directories float to the top, making it obvious where to focus cleanup efforts.
The -h flag for sort requires GNU sort. On BSD systems, you might need to sort numerically first: du -k | sort -rn

5. Show Top 10 Largest Directories

$ du -h | sort -hr | head -10

Chains du, sort, and head to display only the 10 largest directories. This is one of the most useful disk space troubleshooting commands.

Pipeline breakdown: du calculates sizes → sort orders by size → head shows top 10

Use Case: Rapid identification of storage hogs during disk space emergencies. Shows you exactly where to focus your cleanup efforts without scrolling through hundreds of lines.
Create an alias for this: alias ducks='du -h | sort -hr | head -10' (ducks = DU Check Sorted)

6. Exclude Specific Directories

$ du -h --exclude='*.log' --exclude=node_modules

Calculates disk usage while excluding specific patterns or directories. Multiple --exclude flags can be used to skip various paths.

Pattern matching: Supports wildcards (*) and can exclude by filename pattern or directory name.

Use Case: Getting accurate size of source code without build artifacts, measuring data directories without temporary files, or excluding cache directories from reports.
Common exclusions: node_modules, .git, __pycache__, target, build, dist, *.tmp

7. Show Disk Usage with Grand Total

$ du -ch /home/* | tail -1

The -c flag adds a grand total line at the end. Combined with tail, this shows just the total disk usage across multiple directories.

Useful with wildcards: Calculate total size of all items matching a pattern.

Use Case: Getting total size of all user home directories, calculating combined size of multiple backup folders, or summing up space used by related directory trees.

8. Show Disk Usage Including Individual Files

$ du -ah /var/log | head -20

The -a flag shows disk usage for all files, not just directories. Combined with -h for readability, this gives complete file-level detail.

Detail level: Every single file and directory is listed with its size.

Use Case: Finding large individual files (not just directories), identifying which specific log files are huge, or getting complete file-level inventory for forensics.
To find the largest individual files: du -ah | sort -hr | head -20

9. Check Disk Usage with Threshold Filter

$ du -h --threshold=100M

Only displays directories and files larger than the specified threshold. This filters out small items and focuses on significant space consumers.

Threshold units: K (kilobytes), M (megabytes), G (gigabytes), T (terabytes)

Use Case: Finding directories over a certain size (e.g., "show me everything over 1GB"), filtering noise from reports, or creating alerts for directories exceeding size limits.
Use negative thresholds to show items UNDER a size: --threshold=-1M shows items under 1MB

10. Show Apparent Size vs Actual Disk Usage

$ du -h --apparent-size

Shows file sizes rather than disk usage. Without this flag, du shows actual disk space used (including filesystem overhead). With it, you see the file's apparent size.

The difference: A 100-byte file on a filesystem with 4KB blocks uses 4KB on disk but has apparent size of 100 bytes.

Use Case: Matching file sizes reported by other tools (like ls), calculating transfer sizes for backups or network copies, or understanding filesystem efficiency.
Sparse files (files with holes) show smaller apparent size than actual disk usage. This can be confusing when comparing different size metrics.

Quick Reference: Common du Options

Option Description
-h Human-readable output (KB, MB, GB)
-s Summary only (total for directory)
-a Show all files, not just directories
-c Display grand total at the end
-d N or --max-depth=N Limit recursion to N levels deep
--exclude=PATTERN Exclude files matching pattern
--threshold=SIZE Only show items larger than SIZE
--apparent-size Show apparent size instead of disk usage
-0 End lines with null character (for xargs)
-x Stay on same filesystem (don't cross mount points)
-L Follow symbolic links
Useful One-Liners:
  • Find largest directories in /var:
    sudo du -h /var --max-depth=2 | sort -hr | head -20
  • Check all user home directories:
    sudo du -sh /home/*
  • Find directories over 1GB:
    du -h --threshold=1G
  • Exclude hidden files:
    du -h --exclude='.*'
  • Monitor directory size growth:
    watch -n 60 'du -sh /var/log'
Pro Tip for Sysadmins: Create a disk space monitoring script:
#!/bin/bash
# Save as disk-hogs.sh
echo "Top 10 Disk Space Consumers:"
du -h /home /var /opt --max-depth=2 2>/dev/null | sort -hr | head -10
Performance Note: Running du on large directory trees can be slow and CPU-intensive. It reads every file's metadata, which can stress systems with millions of files. Consider using nice or limiting scope during business hours.
Cross-Filesystem Note: By default, du crosses filesystem boundaries. Use -x to stay within a single filesystem: du -hx / won't include mounted filesystems.