ZetCode

Linux du Command

last modified October 23, 2026

The du (disk usage) command in Linux estimates the disk space used by files and directories. While df shows how full a file system is, du shows where the space goes, which makes it the tool for finding large directories and cleaning up disks.

du reports the space actually allocated on disk, which is usually a multiple of the file system block size (4 KiB here), not the length of the file content. The examples use a small project directory and GNU du from coreutils.

Basic usage

Without arguments, du walks the current directory and prints the usage of each subdirectory and the total, in 1K blocks.

du

The last line, ., is the total for the current directory. The size of a directory includes everything inside it. Files are counted but not listed.

$ du
64	./logs
8	./project/docs
48	./project/src
252	./project/build
312	./project
384	.

Human-readable output

The -h option prints sizes with units such as K, M, and G.

du -h

The sizes use powers of 1024. The --si option uses powers of 1000 instead.

$ du -h
64K	./logs
8.0K	./project/docs
48K	./project/src
252K	./project/build
312K	./project
384K	.

Summarizing

The -s option prints only a total for each argument.

du -sh project
du -sh project logs notes.txt
du -sh *

du -sh * is a quick overview of the current directory. Note that the shell wildcard skips hidden files and directories, such as .cache, which can be large.

$ du -sh project
312K	project
$ du -sh project logs notes.txt
312K	project
64K	logs
4.0K	notes.txt
$ du -sh *
64K	logs
4.0K	notes.txt
312K	project

Limiting the depth

The -d (--max-depth) option limits how many levels of directories are printed.

du -h -d 1

With a depth of 1, only the immediate subdirectories and the total are shown. All levels are still counted in the sizes.

$ du -h -d 1
64K	./logs
312K	./project
384K	.

Grand total

The -c option adds a total line for all arguments.

du -ch logs/*.log
du -ch logs/*.log | tail -n 1

Combined with tail, it gives just the total size of a set of files.

$ du -ch logs/*.log
44K	logs/app.log
16K	logs/error.log
60K	total
$ du -ch logs/*.log | tail -n 1
60K	total

Finding the largest files and directories

The -a option includes files in the listing. Sorting the result reveals the largest items.

du -ah | sort -rh | head -n 5

The -h option of sort understands the units printed by du -h. The binary in the build directory takes most of the space.

$ du -ah | sort -rh | head -n 5
384K	.
312K	./project
252K	./project/build
248K	./project/build/app.bin
64K	./logs

Excluding files

The --exclude option skips files matching a pattern.

du -sh --exclude='*.bin' --exclude='*.o' project

Without the compiled files, the project takes only 32K. Quote the pattern so that the shell does not expand it.

$ du -sh --exclude='*.bin' --exclude='*.o' project
32K	project

Size threshold

The -t (--threshold) option hides entries smaller than the given size.

du -h -t 100K

Only directories of at least 100K are listed. A negative value, such as -t -10K, shows entries up to that size instead.

$ du -h -t 100K
252K	./project/build
312K	./project
384K	.

Apparent size versus disk usage

The --apparent-size option reports the length of the file content instead of the allocated space. The -b option reports the apparent size in bytes.

truncate -s 1G disk.img
du -h disk.img
du -h --apparent-size disk.img
du -b notes.txt

truncate creates a sparse file: it is 1 GiB long, but no data blocks are allocated, so it uses no disk space. Conversely, a six-byte file occupies a whole 4K block. Disk images and virtual machine files are often sparse, which explains why ls and du disagree.

$ truncate -s 1G disk.img
$ ls -lh disk.img
-rw-r--r-- 1 jano jano 1.0G Oct 23 12:54 disk.img
$ du -h disk.img
0	disk.img
$ du -h --apparent-size disk.img
1.0G	disk.img
$ du -h notes.txt
4.0K	notes.txt
$ du -b notes.txt
6	notes.txt

Hard links

du counts files with several hard links only once per run.

ln logs/app.log app-link.log
du -ch logs/app.log app-link.log
du -ch app-link.log

Both names refer to the same data, so the second name adds nothing to the total. In a separate run, the link is counted normally. The -l option counts hard links every time.

$ ln logs/app.log app-link.log
$ du -ch logs/app.log app-link.log
44K	logs/app.log
44K	total
$ du -ch app-link.log
44K	app-link.log
44K	total

Modification times

The --time option adds the time of the last modification of any file in each directory.

du -sh --time project/*

This helps to identify large directories that have not changed for a long time and may be candidates for archiving.

$ du -sh --time project/*
252K	2026-10-20 09:30	project/build
8.0K	2026-10-20 09:30	project/docs
48K	2026-10-20 09:30	project/src

Staying on one file system

The -x option skips directories on other file systems, such as mounted disks and virtual file systems.

du -sh /usr/share 2>/dev/null
du -shx / 2>/dev/null

The commands ran in a Debian container. Without -x, measuring / would descend into /proc, network shares, and other mounts. Directories we cannot read produce errors, which we hide by redirecting standard error; run sudo du for exact system-wide numbers.

$ du -sh /usr/share 2>/dev/null
240M	/usr/share
$ du -shx / 2>/dev/null
1.1G	/

Best practices for du

Source

GNU du manual

In this article, we have used the du command to measure the disk usage of files and directories, summarize and sort the results, exclude files, compare apparent size and disk usage, and handle hard links and mount points.

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.