Have you ever encountered a situation where your server suddenly raises a disk alert, and <span>df -h</span> shows that the root partition is 98% full, but you have no idea which directory is consuming the space? After deleting hundreds of MB of log files, the space still hasn’t been freed…
Don’t worry, what you need now is a “detective tool” – the <span>du</span> command.
<span>du</span>, short for disk usage, is one of the most practical directory/file size statistics tools in Linux systems. Unlike <span>df</span>, which tells you “how much is left”, it helps you accurately locate: “who is taking up so much space?”
Today, we will delve into this seemingly simple yet extremely powerful command, allowing you to say goodbye to the anxiety of “disk full”!
1. What is <span>du</span>? Why is it more accurate than <span>ls</span>?
Many people are accustomed to using <span>ls -lh</span> to check file sizes, but when it comes to calculating total directory usage, <span>ls</span> is not reliable. Because:
💡
<span>ls</span>displays the logical size of files (i.e., actual byte count), while<span>du</span>counts the actual disk space used.
For example: a file with only 1KB of content still occupies 4KB of disk space due to the file system’s minimum allocation unit being 4KB (block size). <span>du</span> reflects this 4KB, while <span>ls</span> only shows 1KB.
📌 So:
- If you want to know “how big is this file” → use
<span>ls</span> - If you want to know “how much disk space it occupies” → use
<span>du</span>
2. Basic Syntax and Common Options Overview
du [options] [file or directory]
| Option | Description |
|---|---|
<span>-h</span> |
Human-readable format (K/M/G), most commonly used ✅ |
<span>-s</span> |
Show only total, do not expand subdirectories |
<span>-a</span> |
Show all files and directories (including regular files) |
<span>--max-depth=N</span> |
Control output depth level |
<span>-c</span> |
Add a line for “total” at the end |
<span>-k / -m</span> |
Display in KB or MB respectively |
<span>-S</span> |
Exclude subdirectory totals (only count current level) |
<span>--exclude=</span> |
Exclude certain files/directories (e.g., logs) |
3. Practical Scenarios: 6 High-Frequency Usage Tips
✅ Scenario 1: Quickly check total usage of the current directory
du -sh
👉 Output example:<span>2.3G .</span> indicates that the current directory occupies a total of 2.3GB of space, clear and concise.
✅ Scenario 2: How big is a specific directory?
du -sh /var/log
Commonly used to check if logs have ballooned into “monsters”. If the output is <span>500M</span> or <span>10G</span>, then you should consider log rotation.
✅ Scenario 3: List the sizes of subdirectories in the current directory (one level)
du -sh *
Very suitable for finding out which folder is a “space hog”. For example, if you see <span>node_modules</span> occupying 800M, you know the frontend project is too heavy.
💡 Tip: Combine with sorting for better clarity
du -sh * | sort -hr | head -10
Sort from largest to smallest, taking the top 10 largest items.
✅ Scenario 4: Analyze multi-level directory structures (e.g., two levels under <span>/home</span>)
du -h --max-depth=2 /home
This clearly shows how much space <span>/home/user1/.cache</span> and <span>/home/user2/Downloads</span> occupy, avoiding blind deletions.
✅ Scenario 5: Find the largest 10 files or directories
du -ah | sort -hr | head -10
<span>-a</span>: includes all files<span>-h</span>: human-readable display<span>sort -hr</span>: sort in reverse order by human-readable format
This trick is known as the “wanted list for space killers”!
🎯 Summary: Become a “Sherlock Holmes” of Disk Space
<span>du</span> is simple yet serves as the first line of defense in troubleshooting disk issues. Mastering the following combinations will allow you to easily handle various “space alerts”:
| Scenario | Recommended Command |
|---|---|
| Quickly view current directory size | <span>du -sh</span> |
| Check directory usage | <span>du -sh /path/to/dir</span> |
| List subdirectory rankings | <span>du -sh * | sort -hr</span> |
| Multi-level analysis | <span>du -h --max-depth=2</span> |
| Find largest files | <span>du -ah | sort -hr | head -10</span> |
🔔 Reminder: Don’t forget to pair with
<span>--exclude</span><code><span> to filter out temporary files or logs, for example:</span>du -h --exclude="*.log" --exclude="tmp" /var
From now on, when facing disk alerts, you are no longer the frantic “file deleter”, but a strategic “system detective”!
🎯 Remember: <span>df</span><span> looks at the big picture, while </span><span>du</span><span> identifies the true culprit.</span>
Go ahead and give it a try; you might just find a hidden <span>node_modules</span> monster lurking in your server 😄