How to Use du Command in Linux: Disk Usage Explained
Use the
du command in Linux to check disk usage of files and directories. Run du [options] [path] to see sizes, with options like -h for human-readable output and -s for summary.Syntax
The basic syntax of the du command is:
du [options] [path]
Here, path is the file or directory you want to check. If omitted, it defaults to the current directory.
Common options include:
-h: Show sizes in human-readable format (e.g., KB, MB)-s: Show only the total size for the specified path-a: Show sizes for all files, not just directories--max-depth=N: Limit directory depth shown
bash
du [options] [path]
Example
This example shows how to check the disk usage of the current directory in a human-readable summary:
bash
du -sh .
Output
1.2G .
Common Pitfalls
Common mistakes when using du include:
- Not using
-hand getting sizes in bytes, which are hard to read. - Forgetting
-sand getting a long list of all subdirectories. - Using
duon very large directories without limiting depth, causing slow output.
Example of a wrong and right way:
bash
du . du -sh .
Output
4096 .
2048 ./folder2
6144 .
1.2G .
Quick Reference
| Option | Description |
|---|---|
| -h | Human-readable sizes (KB, MB, GB) |
| -s | Show only total size for each argument |
| -a | Show sizes for all files, not just directories |
| --max-depth=N | Limit output to N levels of directories |
| -c | Display a grand total at the end |
Key Takeaways
Use
du -sh [path] for a quick, readable summary of disk usage.Add
--max-depth=N to limit output depth and speed up results.Without
-h, sizes are shown in bytes and hard to read.Use
-a to see sizes of individual files, not just directories.Avoid running
du on large directories without options to prevent long outputs.