Mastering Disk Space Analysis with the Linux du Command
Core Functionality and Syntax
The du (disk usage) utility is a fundamental POSIX-compliant tool for measuring file and directory storage consumption on Unix-like systems. While filesystem-level reporting tools like df show partition capacity, du traverses directory trees to calculate the actual block allocation for specific paths. This makes it indispensable for capacity planning, log rotation audits, and identifying storage bottlenecks.
The standard invocation follows this pattern:
du [OPTIONS] [TARGET_PATH]
When executed without flags or a target, the utility recursively scans the current working directory and outputs the allocated blocks for every subdirectory it encounters.
Essential Command Flags
Human-Readable Scaling (-h)
Raw block counts are difficult to interpret at scale. The -h flag automatically scales output values into kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T) based on magnitude.
du -h /opt/application
Aggregate Summary (-s)
To suppress recursive listing and retrieve only the total footprint of a target path, use the summarize flag. This is particularly useful for quick capacity checks.
du -s /srv/database
Combining it with human-readable formatting is a common practice:
du -sh /srv/database
Recursion Depth Control (-d or --max-depth)
Deep directory structures can produce overwhelming output. The depth flag restricts traversal to a specified number of levels below the target. Setting it to 1 reveals the size of immediate children without descending further.
du -h -d 1 /var/www
Include Individual Files (-a)
By default, du only reports directory totals. The -a switch forces the utility to list every file alongside its parenet directories, enabling granular storage audits.
du -ah /tmp/build_artifacts
Pattern Exclusion (--exclude)
Storage audits often require ignoring temporary files, caches, or dependency folders. The exclude parameter accepts shell glob patterns to filter out matching paths during traversal.
du -sh --exclude="*.cache" --exclude="vendor" /home/deployer/project
Operational Workflows and Examples
1. Rapid Footprint Assessment
Retrieve a clean, scaled total for a specific service directory:
du -sh /opt/microservices/auth
Expected terminal output:
2.4G /opt/microservices/auth
2. Top-Level Directory Breakdown
Identify which immdeiate subdirectories consume the most space with in a data mount:
du -h -d 1 /mnt/storage
Expected terminal output:
156M /mnt/storage/logs
892M /mnt/uploads
1.1G /mnt/storage/backups
2.1G /mnt/storage
3. Granular File Enumeration
List all files and directories within a target path, scaled for readability:
du -ah /etc/nginx
Expected terminal output:
4.0K /etc/nginx/mime.types
8.0K /etc/nginx/conf.d/default.conf
12K /etc/nginx/conf.d
24K /etc/nginx
4. Targeted Audit with Filters
Calculate the size of a project directory while ignoring compiled assets and log files:
du -sh --exclude="*.log" --exclude="dist" /var/apps/frontend
Expected terminal output:
340M /var/apps/frontend