0
0
Linux-cliHow-ToBeginner · 3 min read

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 -h and getting sizes in bytes, which are hard to read.
  • Forgetting -s and getting a long list of all subdirectories.
  • Using du on 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

OptionDescription
-hHuman-readable sizes (KB, MB, GB)
-sShow only total size for each argument
-aShow sizes for all files, not just directories
--max-depth=NLimit output to N levels of directories
-cDisplay 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.