ZetCode

Linux du Command

last modified October 18, 2026

The du command estimates the disk space used by files and directories. Unlike df, which reports whole file systems, du works from the directory tree and shows how much space each file or directory consumes. This tutorial covers basic and advanced usage of du with practical examples.

du is commonly used to find large directories, check how much space a project or log directory uses, and clean up disk space.

The examples use GNU du. Some options and defaults differ in other implementations.

Basic Usage

Without options, GNU du walks the current directory and prints the disk usage of every subdirectory, normally in 1 KiB units. Environment variables can change the default unit; use du -k to request KiB explicitly.

du_basic.sh
$ du
76      ./logs
16      ./projects/docs
252     ./projects/src
272     ./projects
352     .

Each number is the space used by that directory and everything below it, in KiB with the normal default settings. The final line is the total for the current directory. Because directories are listed from the bottom up, the same files are included in both their own directory's total and its ancestors' totals; adding all the output lines would double-count their usage.

Human-Readable Output

The -h option adds unit suffixes such as K, M, and G.

du_human.sh
$ du -h
76K     ./logs
16K     ./projects/docs
252K    ./projects/src
272K    ./projects
352K    .

Human-readable output is easier to scan, but avoid -h when the output is parsed by a script. Use -k for allocated usage in KiB or --block-size=1 for allocated usage in bytes. Use -b when you specifically want apparent size in bytes; it is equivalent to --apparent-size --block-size=1.

Summarize a Directory

The -s option prints only a total for each argument instead of every subdirectory.

du_summary.sh
$ du -sh projects
272K    projects

Combine -s with -h for the most common disk-usage check. The argument can be one directory, several directories, or a set of files.

Limit the Directory Depth

The --max-depth option controls how many directory levels are printed. A depth of 1 shows only the immediate children.

du_depth.sh
$ du -h --max-depth=1 .
76K     ./logs
272K    ./projects
352K    .

The short form -d 1 is equivalent. This is useful for finding which top-level directory is responsible for most of the usage without printing thousands of nested entries.

--max-depth limits which directory totals are printed, not how deeply du traverses. Deeper directories are still examined to calculate those totals.

Show a Grand Total

The -c option adds a total line at the end. It is often combined with a list of files or directories.

du_total.sh
$ du -ch logs/*.log | tail -n 1
72K     total

Without tail, the output also shows the size of each file. The -c total is the sum of the listed arguments, not the total of the whole file system.

Find the Largest Files and Directories

The -a option includes regular files, not just directories. Piping the output to sort -rh ranks entries from largest to smallest.

du_largest.sh
$ du -ah . | sort -rh | head -n 6
352K    .
272K    ./projects
252K    ./projects/src
248K    ./projects/src/app.bin
76K     ./logs
52K     ./logs/app.log

The -h option of sort understands human-readable suffixes. Add --max-depth=1 to exclude nested entries when you only care about top-level directories.

Exclude Files or Directories

The --exclude option skips paths that match a shell pattern. The pattern is matched against the path being examined.

du_exclude.sh
$ du -sh --exclude='*.log' .
280K    .

Use quotes so the shell does not expand the pattern. You can repeat --exclude for several patterns. Excluded files are not counted in the totals of their parent directories.

Stay on One File System

The -x option does not descend into directories on other mounted file systems.

du_onefs.sh
$ du -sh -x /var
2.1G    /var

This is useful when a directory contains mount points for external disks, network shares, or containers. Without -x, those file systems would be included in the total.

Apparent Size versus Disk Usage

By default, du reports allocated disk blocks. The --apparent-size option reports the logical file size instead.

du_apparent.sh
$ du -sh --apparent-size projects
266K    projects

The two values can differ because of block rounding, sparse files, compression, or copy-on-write file systems. Use the default to report allocated blocks. On file systems with shared extents or compression, these totals do not necessarily represent uniquely consumed space or the space that deleting the files would reclaim.

Best Practices for du

Source

GNU du Manual

In this article, we have explored various examples of using the du command for estimating directory sizes, limiting depth, calculating totals, excluding paths, and finding the largest files.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Linux tutorials.