Terminal GuideTerminal Guide

du Command Guide

The du (disk usage) command estimates file and directory space usage. Learn how to find what's consuming your disk space.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

duDirectory usage
du -hHuman readable
du -sSummary total

Options

-d 1Depth level 1
-cShow grand total
-aInclude files

Common

du -sh *Size of each item
du -sh * | sort -hSorted by size
du -sh . --exclude=dirExclude dir

Downloadable Image Preview

Failed to generate preview

Basic Usage

du shows disk usage of files and directories.

bash
# Current directory
du

# Specific directory
du /path/to/directory

# Specific file
du file.txt

Common Options

du Options

-hHuman-readable sizes (K, M, G)
-sSummary only (total size)
-aInclude files, not just directories
-cShow grand total
-d NLimit depth to N levels
--max-depth=NSame as -d N
-xStay on one filesystem
--excludeExclude matching files

Human-Readable Output

bash
# Human-readable sizes
du -h

# Summary with human-readable
du -sh directory/

# All files and directories
du -ah

Summary and Totals

bash
# Total size of directory
du -sh /home/user

# Total of multiple directories
du -sh dir1 dir2 dir3

# With grand total
du -ch dir1 dir2 dir3

Limiting Depth

bash
# One level deep
du -h -d 1

# Two levels deep
du -h --max-depth=2

# Just immediate subdirectories
du -sh */

Practical Examples

Find largest directories

bash
du -h -d 1 | sort -hr | head -10

Find largest files

bash
du -ah | sort -hr | head -20

Check home directory usage

bash
du -sh ~/*

Find space hogs in root

bash
sudo du -h -d 1 / 2>/dev/null | sort -hr | head -10

Exclude certain directories

bash
du -h --exclude="node_modules" --exclude=".git" -d 1

Compare directory sizes

bash
du -sh project_v1 project_v2

Find directories over 1GB

bash
du -h -d 2 | grep -E '^[0-9.]+G'
Tip
Use -x to stay on one filesystem and avoid crossing mount points when checking root directories.

du vs df

du and df serve different purposes:

Comparison

duDisk Usage - size of files/directories
dfDisk Free - filesystem space availability
bash
# How much space does /home use?
du -sh /home

# How much space is free on the filesystem?
df -h /home

Interactive Tools

For interactive exploration, consider these tools:

bash
# ncdu - NCurses Disk Usage
sudo apt install ncdu
ncdu /

# dust - Modern du alternative (Rust)
dust

# duf - Modern df alternative
duf
Info
ncdu provides an interactive interface that makes it easy to navigate and find large directories.

Apparent Size vs Disk Usage

bash
# Disk usage (actual blocks used)
du -sh file.txt

# Apparent size (logical size)
du -sh --apparent-size file.txt

Sparse files and filesystem block sizes can cause these to differ.

Summary

du is essential for managing disk space. Key takeaways:

  • Use du -sh for directory summary
  • Use du -h -d 1 for one level deep
  • Use du | sort -hr to find largest items
  • Use --exclude to skip directories
  • Consider ncdu for interactive exploration

Related Articles