Disk usage with df and du

Intermediate · 20 min

Find out how full a disk is with df and what filled it with du. Covers -h, -s, depth limits, sorting, and the BSD flags that differ on macOS.

Before thisListing and inspecting filessort, uniq, cut, tr and wcArchives with tar and gzip

Learn Linux TV · 20 min

Disk usage with df and du

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • df reports per filesystem: size, used and free. du walks a directory and adds up what its files occupy. You use df to learn that a disk is filling and du to learn what filled it.
  • -h prints human-readable sizes and -s on du prints one total per argument. du -sh * | sort -rh ranks a directory's entries by size.
  • The depth flag differs: BSD and macOS du take -d N and reject --max-depth=N, while GNU du takes --max-depth=N and also -d N.
  • du counts disk blocks, not bytes. A 3-byte file shows as 4K, and a sparse file can have a huge apparent size with almost no blocks used.
  • Free space on macOS APFS is shared across volumes, so Used plus Avail need not equal Size.

Two different questions

A disk fills up and something breaks. Builds fail, a database stops writing, logs vanish. The first question is simply how much room is left. The second, and the harder one, is where it went.

The tools answer one each. df (“disk free”) asks the filesystem and reports totals per mounted filesystem, which is cheap because the filesystem already knows. du (“disk usage”) has no total to consult. It walks a directory tree, adds up every file, and reports the sum, which can take a while on big trees.

I ran these on macOS, so the flags and output are the BSD versions. Where Linux differs, I say so, and I did not run it there. The directory I measured was a scratch tree with a 3 MB file, a 500 KB file, a 20 KB file and a tiny text file.

df: how full is it

$ df -h .
Filesystem      Size    Used   Avail Capacity iused ifree %iused  Mounted on
/dev/disk3s5   228Gi   132Gi    71Gi    65%    2.2M  749M    0%   /System/Volumes/Data

Giving it a path (.) reports the filesystem that path lives on, which beats scanning a long list. -h is human-readable, using powers of 1024. Plain df reports 512-byte blocks on macOS. df -k gives 1024-byte blocks, and adding -P gives a fixed one-line-per-filesystem POSIX layout that scripts can parse:

$ df -kP . | head -2
Filesystem   1024-blocks      Used Available Capacity  Mounted on
/dev/disk3s5   239362496 138753832  74931268    65%    /System/Volumes/Data

Notice that 132 plus 71 is not 228. On APFS, several volumes share one pool of space, so Used counts this volume and Avail counts what the whole container still has. On a classic Linux filesystem, Size is roughly Used plus Avail plus a reserved slice. The inode columns (iused, ifree) matter too, since a filesystem can be “full” because it ran out of file entries while having bytes to spare, usually from millions of tiny files.

du: what took the space

du with no flags prints every directory, which is a flood. Start with -s for one summary per argument and -h for readable units:

$ du -sh a b c
3.3M	a
 20K	b
4.0K	c

To see one level down, limit the depth. This is where the flavors split:

$ du -h -d 1 .
3.3M	./a
4.0K	./c
 20K	./b
3.4M	.
$ du -h --max-depth=1 .
du: unrecognized option `--max-depth=1'

BSD du takes -d N. GNU du takes --max-depth=N, and I believe it also accepts -d N, but I did not run it, so check man du. Neither listing is sorted. Feed it to sort:

$ du -sh * | sort -rh | head -4
3.3M	a
 20K	b
4.0K	c
  0B	sparse.img

sort -h understands the K, M and G suffixes, and -r puts the biggest first. This worked with the sort on this Mac. -h on sort is GNU-originated and newer macOS releases include it, so on an older system use du -sk and sort -n instead:

$ du -sk * | sort -rn | head -3
3424	a
20	b
4	c

Descend into the biggest one, and repeat. That is the whole technique: du -sh * at the top, then cd into the winner.

Two more flags I used. -x stays on one filesystem, so a du of / does not wander into mounted drives. -c adds a grand total:

$ du -ch a/big.bin b/small.bin | tail -3
2.9M	a/big.bin
 20K	b/small.bin
2.9M	total

An unreadable directory is reported, not skipped silently:

$ du -sh noperm
du: noperm: Permission denied

Blocks, not bytes

du shows what the filesystem has allocated, which is in whole blocks:

$ ls -l c/tiny.txt | awk '{print $5" bytes by ls"}'
3 bytes by ls
$ du -h c/tiny.txt
4.0K	c/tiny.txt
$ du -A -k c/tiny.txt
1	c/tiny.txt

The file is 3 bytes long and occupies a 4K block. BSD du -A reports apparent size, rounded up to the unit. GNU du uses --apparent-size for that, and I did not run it.

The opposite case is a sparse file, which promises a size without using disk until data is written:

$ dd if=/dev/zero of=sparse.img bs=1 count=0 seek=100m
$ ls -l sparse.img | awk '{print $5" bytes by ls"}'
104857600 bytes by ls
$ du -h sparse.img
  0B	sparse.img

ls says 100 MB and du says 0B. Virtual-machine disks and database files are often sparse, so a file that “looks” huge may cost nothing, and df is the one that reflects actual usage. If du totals look lower than df says is used, look for deleted files that a running process still holds open, which keep their space. I did not reproduce that case here.

Habits

  • df -h . before and after a big operation.
  • du -sh * | sort -rh | head is the find-what-grew command.
  • For a full interactive tree there are tools such as ncdu, which I did not run.
  • A du over a large tree can be slow. Run it on the subtree you suspect first.

About the video

“Linux Crash Course - The df and du Commands” is a 20-minute 27-second video from Learn Linux TV (Jay LaCroix), published June 2022. The description says it covers how to see how much storage is left and how to find which directories use the most space. It has no chapters and I could not read a transcript. It is an English-language channel. It is shot on Linux, so the GNU long options such as --max-depth that it may use are the ones that fail on a Mac as shown above.

Where this fits

You can now measure what a directory holds. Next is comparing two versions of text, which is how a change gets reviewed or shipped: diff and patch.

Check yourself

  1. 01A deploy fails with 'no space left on device'. Which command do you run first and which second?

    df -h on the path first, to confirm the filesystem is full or out of inodes. Then du -sh * | sort -rh in that tree to find what is large.

  2. 02On a Mac, du -h --max-depth=1 . errors. What works?

    du -h -d 1 . The -d flag is BSD du's depth option. GNU du also accepts it, so it is the portable choice.

  3. 03ls -l says a file is 100 MB but du -h says 0B. How?

    It is a sparse file. Its apparent size is 100 MB but almost none of it is backed by disk blocks. du counts blocks actually allocated.

  4. 04A directory shows 4.0K under du and has one 3-byte file. Why is it not 3 bytes?

    Filesystems allocate space in blocks, so even a tiny file occupies a whole block. du reports allocation, not content length.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark