Skip to content

du — Estimate File Space Usage

Overview

du (disk usage) measures and reports the disk space occupied by files and directory trees. It is essential for identifying which directories or log files are consuming server disk space.

Command type: External (GNU coreutils)
Location: /usr/bin/du
Standard: POSIX.1-2017


Syntax

du [OPTIONS] [PATH...]

Options Reference

Option Description
-h --human-readable — Print sizes in human-readable units (K, M, G)
-s --summarize — Display only a total size for each argument
-d N --max-depth=N — Print total size for directories only down to depth N
-a --all
-c --total
-x --one-file-system
--exclude=PATTERN Exclude files matching PATTERN

Examples

Check Total Size of Current Directory

du -sh .

Find Top 10 Largest Directories in /var

du -h -d 1 /var | sort -h -r | head -10

Check Depth 1 Sizes in /var/log

du -h --max-depth=1 /var/log

Sample output:

1.2G    /var/log/nginx
450M    /var/log/journal
120M    /var/log/apt
1.8G    /var/log

Exclude Specific Patterns

# Exclude node_modules and .git folders
du -h --max-depth=1 --exclude="node_modules" --exclude=".git" /projects

Interview Questions

How do you find the 10 largest subdirectories under /var using du?

du -h -d 1 /var | sort -hr | head -10
-d 1 restricts depth to 1 level, sort -hr sorts human-readable numbers in reverse (largest first), and head -10 outputs the top 10.


Command Description
df Report filesystem disk space
ncdu NCurses interactive disk usage analyzer
find Search for files by size (find . -size +100M)