📂 Command: du (Disk Usage)
While df shows you the big picture of your partitions, du is the microscope. It tells you exactly how much space individual files and directories are consuming.
1. The "Why"
If df warned you that your drive is 95% full, du is the tool you use to find the culprit.
- Locate "Space Hogs": Find out if it’s your Android Studio builds, your LaTeX logs.
- Project Management: Check the total size of your Project source code folder before backing it up.
- System Cleanup: Identify large log files in
/var/logthat might need rotating.
2. Basic Syntax
du [OPTIONS] [PATH]
Running du by itself will list every single subfolder in your current directory—which is usually a wall of text. You almost always want to use specific flags to make it readable.
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-h |
Human Readable | Shows sizes in K, M, or G instead of raw bytes. |
-s |
Summary | Shows only the total for the specified folder, not every subfolder. |
-d [N] |
Max Depth | Limits how many levels of subfolders it shows (e.g., -d 1). |
-a |
All | Includes individual files in the count, not just directories. |
-c |
Grand Total | Adds a "total" line at the very bottom. |
4. Practical Examples
A. Checking the Total Size of a Folder
To see exactly how much space your e-book project is taking:
du -sh ./ecommerce-blueprint
B. Finding the Heaviest Folders (Depth 1)
If you want to see which folder in your Home directory is the largest without seeing every single sub-item:
du -h -d 1 ~
C. Sorting the "Top 10" Largest Folders
This is a classic troubleshooting command. It lists all folders, sorts them by size, and shows the top 10:
du -ah . | sort -rh | head -n 10
5. Visualizing Disk Usage
While du is great for text, sometimes seeing the hierarchy helps you understand where the "bloat" is coming from.
6. Pro-Tips
- The Difference: Remember:
df= Disk Free (The whole drive);du= Disk Usage (A specific folder). - Permission Denied: Just like with
find, if you runduon system folders (like/varor/root), you should usesudoto get an accurate count. - Arch Linux Context: Use
du -sh /var/cache/pacman/pkgto see exactly how many Gigabytes of old updates are sitting on your drive. You might be surprised!