Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Disks and Filesystems

Linux du command

Reference for the Linux du command: sum disk usage of files and directories, use -s, -h, -d and --max-depth, exclude paths, and understand apparent size versus disk use.

du ("disk usage") walks a directory tree and adds up the space used by the files in it. It is the tool for the question "what is eating my disk?": run it on a directory, sort the result and the biggest subdirectories are at the top.

By default du reports disk blocks actually allocated, which can differ from the length of the file: a sparse file takes less than its size, and a one-byte file still takes a whole block. With --apparent-size it reports file lengths instead.

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

Try it

Bash Apparent size, summary and depth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Sparse files and real disk use

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
du [-a | -s] [-h] [-c] [-d N] [-x] [-L] [--apparent-size] [--exclude=PATTERN] [-B SIZE] [FILE]...
Option Description
-s, --summarize Print only a total for each argument.
-a, --all Show files as well as directories.
-h, --human-readable Sizes like 4.0K, 1.2G (powers of 1024). --si uses powers of 1000.
-d N, --max-depth=N Print totals only for directories up to N levels deep. -d 0 is the same as -s; -d 1 lists immediate subdirectories.
-c, --total Add a grand total line.
-x, --one-file-system Stay on one filesystem; do not descend into other mounts.
-b, --bytes Same as --apparent-size -B1.
--apparent-size Count file lengths, not allocated blocks.
-k, -m, -B SIZE Choose the unit.
--exclude=PATTERN Skip files and directories matching a glob. Repeatable.
-L Follow symbolic links (by default only the link itself is counted).
-l Count hard-linked files each time they appear.
--time Show the latest modification time in the tree.
-0 End each output line with a NUL byte.

Returns: 0 on success; 1 if any file or directory could not be read (permission denied, vanished while scanning). Results for the readable parts are still printed.

Usage notes

  • The classic cleanup command: du -h --max-depth=1 /var | sort -h lists the biggest subdirectories last. sort -h understands K, M, G suffixes. Add | tail to see the top entries.
  • Directories count too. In apparent-size mode each directory adds its own entry size (often 4096 bytes), so a tree total is slightly larger than the sum of its files.
  • Hard links are counted once per du run: the first path that reaches the inode is charged, later paths show no extra size. This makes the per-directory numbers depend on traversal order.
  • Disk blocks, not bytes. On a filesystem with 4 KiB blocks, a 1-byte file shows 4. A large number of small files therefore uses more disk than their total length suggests.
  • Compared with df. du sees files you can reach; df reports the filesystem. Space held by deleted-but-open files, other users' unreadable directories and filesystem metadata explain differences.
  • Permission errors. Run as root (or accept the "Permission denied" lines) for system directories. Redirect with 2>/dev/null to hide them, though you then undercount.
  • Mount points. Without -x, du / descends into /proc, /sys and network mounts. Use du -x / for the root filesystem only.
  • Speed. du reads metadata for every file, so a huge tree takes time and cache. Run it on the specific directory you suspect. Tools like ncdu offer an interactive version, but are not part of coreutils.
  • Portability. --max-depth and --apparent-size are GNU options; BSD du uses -d and has no --apparent-size.