Skip to content

du Command Cheat Sheet

du (disk usage) estimates file space usage. Unlike ls -l which shows file size, du shows the actual disk allocation (blocks used), making it more accurate for finding what's consuming your storage.


Synopsis

du [OPTION]... [FILE]...

Basic Usage

Summarize Directory Size (-s)

Display only the total size of the specified directory, not its subdirectories.

du -s /home/user/projects

Human-Readable Format (-h)

Show sizes in K, M, G (1024 base).

du -sh /home/user
# Output: 15G    /home/user

SI Units (-H / --si)

Show sizes in powers of 1000 (KB, MB, GB).

du -sH /home/user

Analyzing Subdirectories

Max Depth (--max-depth)

Instead of listing every single subdirectory recursively, limit the depth.

# Only show immediate subfolders (Depth 1)
du -h --max-depth=1 /var

All Files (-a)

By default, du only lists directories. Use -a to show individual files too.

du -ah /path/to/dir

Sorting and Finding Large Files

du does not sort by size natively (it sorts by name). Combine with sort.

Find Top 10 Largest Directories

du -h --max-depth=1 | sort -hr | head -n 10
  • sort -h: sort numbers with human suffixes (K, M, G).
  • sort -r: reverse (largest first).

Find Largest Files in a Tree

du -ah /var/log | sort -hr | head -n 10

Excluding Files

Exclude by Pattern (--exclude)

Ignore files matching a wildcard pattern.

# Calculate size but ignore all .log files
du -sh --exclude="*.log" /var/www

Exclude From File (-X)

Read exclude patterns from a file.

du -sh -X patterns.txt /path

Output Formatting

Timestamp (--time)

Show the modification time of the files/directories.

du -h --time /path

Apparent Size (--apparent-size)

Print apparent sizes instead of disk usage. Use this to see "logical" size rather than "physical" block usage (useful for sparse files).

du -h --apparent-size file.img

Null-Terminated Output (-0)

End each output line with specific 0 byte output instead of newline for piping to xargs.

du -0 /path | xargs -0 rm

Grand Total (-c)

Produce a grand total line at the end of the output.

du -shc dir1/ dir2/ dir3/
Output:
1.2G    dir1/
500M    dir2/
2.0G    dir3/
3.7G    total


Practical Examples

Check Sizes of Specific Files

du -h file1.iso file2.zip

Find Hidden Directories Consuming Space

du -sh .[!.]* | sort -hr

Compare du vs df

  • du: Sums up file sizes. Can be lower than df if files are deleted but held open by processes.
  • df: Reads filesystem metadata. Shows total used blocks on the device.

If df says disk is full but du doesn't see it:

lsof +L1
(Lists open files that have been unlinked/deleted).


Exit Status

Code Meaning
0 Success
1 Error (Permission denied, file not found)

Notes

  • du works recursively by default.
  • It may take a long time on huge filesystems; use ncdu (Ncurses Disk Usage) for a faster, interactive alternative if available (apt install ncdu).