Sort and Display Directories by Disk Usage (du + sort)
When disk space runs low on a Linux server, this guide covers the du + sort combination for quickly identifying which directories are the largest.
[01] Check the Current Directory’s Usage
1-1. Basic Command
Lists the subdirectories of the current directory ordered from largest to smallest.
1
du -h --max-depth=1 | sort -hr
| Option | Description |
|---|---|
du -h |
Display sizes in human-readable units (K, M, G) |
--max-depth=1 |
Only show one level below the current directory |
sort -h |
Sort considering human-readable units (K < M < G) |
sort -r |
Descending order (largest first) |
Example output:
1
2
3
4
5
15G .
8.2G ./data
4.1G ./logs
2.3G ./backup
512M ./config
1-2. Targeting a Specific Directory
Replace /var with any path to analyze that directory.
1
du -h --max-depth=1 /var | sort -hr
[02] Filtering Directories Only
2-1. Exclude Files, Show Only Directories
Add grep to filter out files and keep only directory entries.
1
du -h --max-depth=1 | grep '/$' | sort -hr
grep '/$' filters only entries ending with / (directories).
2-2. Adjusting Depth
Increasing --max-depth lets you inspect deeper subdirectories.
1
2
# Check up to 2 levels deep
du -h --max-depth=2 | sort -hr | head -20
Appending head -20 displays only the top 20 entries for better readability.
[03] Practical Examples
3-1. Finding Large Folders from the Root Directory
The first command to run when disk space is tight.
1
sudo du -h --max-depth=1 / | sort -hr | head -15
Scanning root (/) requires sudo and may take a while.
3-2. Cleaning Up the Home Directory
1
du -h --max-depth=1 ~ | sort -hr
3-3. Inspecting the Log Directory
1
sudo du -h --max-depth=2 /var/log | sort -hr | head -10