Bash Directory Usage (du)
Monitor disk space usage of files and directories
📊 What is du Command?
The du (disk usage) command displays the amount of disk space used by files and directories. It helps you find which folders are taking up the most space on your system.
# Check disk usage of current directory
du -h
# Output shows size and directory name
# 4.0K ./folder1
# 8.0K ./folder2
Output:
4.0K ./folder1 8.0K ./folder2 12K .
Common du Options
Human Readable
Display sizes in KB, MB, GB format
du -h /home/user
Summary
Show total size only
du -sh /var/log
Max Depth
Limit directory depth to scan
du -h --max-depth=1
Sort by Size
Find largest directories
du -h | sort -rh
🔹 Basic du Usage
Running du in a directory without options recursively calculates and displays the disk space used by every subdirectory and file it contains. The output shows sizes in kilobytes by default, listing the consumption of each item. This provides a detailed, if sometimes overwhelming, view of where space is allocated. The basic command, like du /var/log, is the first step in any disk space investigation, helping you quickly map out the storage footprint of a specific location and identify potential areas for cleanup or archiving.
# Check current directory size
du
# Check specific directory
du /home/user/Documents
# Human-readable format
du -h /home/user/Downloads
Output:
128K /home/user/Documents/reports 256K /home/user/Documents/images 512K /home/user/Documents
🔹 Summary of Directory Size
To get a concise total for a directory without the verbose listing of every sub-item, use the -s (summarize) option with du. For instance, du -sh /home/user will output a single line showing the aggregate size of the entire /home/user directory in human-readable format (-h). This is immensely useful when you only need the overall footprint—such as checking the total size of a project folder, a user's home directory, or a backup location—enabling quick assessments without sifting through pages of detailed data.
# Get total size of directory
du -sh /var/log
# Check multiple directories
du -sh /home /var /tmp
# Summary with human-readable sizes
du -sh ~/Documents ~/Downloads ~/Pictures
Output:
2.5G /var/log 15G /home 1.2G /var 500M /tmp
🔹 Limiting Directory Depth
Control the recursion depth of the du command with the --max-depth=N option to prevent information overload and focus on top-level consumers. Setting --max-depth=1 shows sizes for only the immediate subdirectories within the specified path. Increasing it to 2 goes one level deeper. This allows you to perform a structured analysis: start with depth 1 to find the problematic top-level folder, then drill down further. It’s an essential technique for efficiently navigating large directory trees like /var or /usr during storage audits.
# Show only first level subdirectories
du -h --max-depth=1 /home/user
# Show two levels deep
du -h --max-depth=2 /var
# Current directory, one level only
du -h --max-depth=1
Output:
512M /home/user/Documents 1.5G /home/user/Downloads 256M /home/user/Pictures 2.3G /home/user
🔹 Finding Largest Directories
Combine du with the sort command to systematically identify the biggest space hogs on your system. A powerful pipeline like du -h --max-depth=1 / | sort -rh scans the root directory, outputs human-readable sizes for top-level folders, and then sorts them in reverse order, placing the largest consumers at the top. This method is a cornerstone of disk cleanup procedures, allowing administrators and users to instantly see which directories (e.g., /var, /home) are responsible for the majority of used space and require immediate attention.
# Sort by size, largest first
du -h /home/user | sort -rh | head -10
# Find top 5 largest directories
du -sh /var/* | sort -rh | head -5
# Check current directory
du -h --max-depth=1 | sort -rh
Output:
2.3G /home/user 1.5G /home/user/Downloads 512M /home/user/Documents 256M /home/user/Pictures 128M /home/user/Videos
🔹 Exclude Files and Directories
The --exclude parameter lets you skip specific patterns (like file types or directory names) when du calculates disk usage, leading to more focused and relevant results. For example, du -h --exclude="*.log" --exclude="cache" /var would estimate space while ignoring all .log files and any directories named 'cache'. This is crucial for excluding volatile or non-essential data (temporary files, caches, specific backups) from your analysis, ensuring you get an accurate picture of persistent storage consumption that matters for long-term cleanup decisions.
# Exclude specific directory
du -h --exclude="*.log" /var
# Exclude multiple patterns
du -h --exclude="node_modules" --exclude=".git" ~/projects
# Exclude cache directories
du -sh --exclude="cache" /home/user
Output:
1.2G /var (excluding .log files) 850M ~/projects (excluding node_modules and .git) 1.8G /home/user (excluding cache)
🔹 Practical Examples
Everyday uses of head include validating data files, monitoring log heads, and creating data samples. A data scientist might run head -1000 large_dataset.csv > sample.csv to create a manageable sample for testing scripts. A developer might use head -20 application.log to check recent activity after a deployment. These scenarios highlight head's role as a gatekeeper, providing a safe, controlled way to interact with the beginning of files and streams, which is often where the most current or defining information resides.
# Find what's filling up your home directory
du -sh ~/* | sort -rh | head -10
# Check log file sizes
du -sh /var/log/*
# Monitor project directory sizes
du -h --max-depth=1 ~/projects | sort -rh
# Find large hidden directories
du -sh ~/.[!.]* | sort -rh
Output:
5.2G /home/user/Downloads 2.1G /home/user/Videos 1.8G /home/user/Documents 950M /home/user/.cache 500M /home/user/Pictures