$_ CLI In Depth
Learning progress 0%
lesson disks mounts archives

Disk Usage: df & du

5 min read Module 12: Disks, Mounts & Archives Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:13

Two tools, two questions

df, short for disk free, reports the state of each mounted filesystem. It asks the filesystem for its total size, how much is in use, and how much is available to ordinary users. It does not walk your files. du, short for disk usage, walks a directory tree and adds up the space each file occupies. It is a measurement of paths, and it costs time proportional to the number of files.

The mismatch between the two is normal. A file that was deleted while a program still has it open takes space that du cannot see but df does count. A directory tree that is a mount point is counted by du only if you cross into it. Each tool is right about its own question.

Reading df

Pass a path to df and it reports the filesystem that holds that path. The -P flag selects the POSIX output format, which uses one line per filesystem and fixed column order, so it is safe to parse in scripts. The -h flag makes sizes human-readable for people, and it is the format you usually want at a terminal.

BASH2 lines
df -P /workspace
df -h /workspace

The first column is the filesystem source, the second is the total size in 1024-byte blocks, the third is the amount used, the fourth is the amount available, and the last column is the mount point. In the lab, the source is overlay and the mount point is /, because the root overlay holds the workspace. The total and available numbers are small, so the lab is a good place to watch them change.

Tip

Use df -i when a disk reports free space but writes still fail with "No space left on device". That message can also mean the filesystem has run out of inodes, the records that describe files, and df -i shows that count separately.

Reading du

du with no options prints the size of every directory under the path it is given. That output is noisy in a large tree, so most people add -s to print a single total and -h for readable units. The --apparent-size flag reports the size of the file data as the program sees it, not the number of blocks the storage uses, and the -b flag is shorthand for bytes with apparent size.

BASH2 lines
du -sh /usr/bin
du -sb --apparent-size /workspace

The difference between blocks and apparent size matters for sparse files. A file can claim a size of one gigabyte while occupying almost no blocks, because most of it is holes that were never written. The block count reflects storage, and the apparent size reflects what a program that reads the file will see. For ordinary files on the lab the two numbers are close.

Finding the big things

When a disk is full, the usual pattern is to ask du for the largest entries one level down and then descend into the largest one. Sorting numerically on the first column does the job. The pattern uses only standard tools, so it works on any system.

BASH1 lines
du -s /usr/* 2>/dev/null | sort -n | tail -3

The sort is numeric, so the last three lines are the three largest directories. You can repeat the command on one of them to go deeper. The 2>/dev/null part discards permission errors for directories you cannot read, which otherwise clutter the output.

Why the numbers disagree

Beginners often expect df and du to agree, and when they do not, they assume one of them is broken. Several ordinary causes explain the gap. Reserved blocks hold back a share of a filesystem for privileged processes, so df can report a disk as full for ordinary users while some space remains. Filesystem metadata, such as the journal and the inode tables, takes space that no file size includes. A file removed while a process still holds it open keeps its blocks until that process closes it.

Overlay and tmpfs add another layer of interpretation. The space df reports for an overlay is the space of the filesystem underneath it, and a tmpfs reports the limit set by its mount options. A tree that lives on such a filesystem can therefore be far smaller than the free space suggests, or the reverse, depending on which layer you measure.

The safe habit is to decide the question before you run a command. If you want to know which directory is large, ask du. If you want to know whether a filesystem is about to fail, ask df, and check df -i as well. Both tools are correct about what they measure, and most confusion comes from asking one of them the other tool's question.

Mounts and du

By default, du counts everything under the path it is given, including other filesystems mounted inside it. The -x flag stops it at filesystem boundaries, so a tree that includes a mounted disk reports only the files on the filesystem you asked about. This is useful when the answer should describe one device and not the whole tree.

Hands-on: measure a tree you control

Build a directory whose size you know, then check it with both tools. The file sizes come from head reading from /dev/zero, which produces an exact number of zero bytes. The directory will contain one megabyte and two kilobytes of data, so its apparent size is at least that many bytes.

BASH4 lines
mkdir -p data
head -c 1048576 /dev/zero > data/one.bin
head -c 2048 /dev/zero > data/two.bin
du -sb --apparent-size data

The output of the last command is at least 1050624, which is the sum of the two files. It can be larger by the size of the directory entry itself, which is the detail that separates a careful reading from a casual one. Compare the apparent size with what df -P reports for the filesystem, and notice that df counts blocks shared by the whole filesystem while du counts only your tree.

Terminal lab · 3 steps · real Linux in your browser

Measure space two ways

Create a tree of known size, then measure it with df and du.

01
Find the filesystem behind /workspace
df -P prints one line per filesystem in a fixed format. The first line is a header, and the second line describes the filesystem. Save the first column of that second line in df-source.txt. ``bash df -P /workspace | awk 'NR == 2 { print $1 }' > df-source.txt ``
Show hint for step 1
Use NR == 2 in awk to skip the header, and print field 1, the source.
02
Create a tree of known size
Create a directory named data with two files of exact sizes. The first is one mebibyte of zero bytes, and the second is 2048 bytes. Use head reading from /dev/zero. ``bash mkdir -p data head -c 1048576 /dev/zero > data/one.bin head -c 2048 /dev/zero > data/two.bin ``
Show hint for step 2
head -c N /dev/zero writes exactly N zero bytes. 1 MiB is 1048576 bytes.
03
Measure the apparent size
Ask du for the apparent size of data in bytes, with one total and no per-directory lines. Save the number in data-bytes.txt. The total must be at least the sum of the two files. ``bash du -sb --apparent-size data | cut -f1 > data-bytes.txt ``
Show hint for step 3
-s gives one total, -b gives bytes, and --apparent-size reports file data. cut -f1 keeps only the number.
Check yourself Question 1 of 4
Concept Check Tool for filesystem fullness recall

Which tool reports the total, used, and available space of a mounted filesystem without walking its files?

Recap & Key Takeaways

df reports the size and use of a whole filesystem, while du sums the files under a path, so their numbers answer different questions.
df -P gives a stable one-line-per-filesystem format for scripts, and df -i shows inode use when space is reported but writes fail.
du -sh gives a readable total, du -sb --apparent-size gives the size of the data, and du -x stays on one filesystem.
Sort the output of du -s with sort -n to find the largest directories when a disk is full.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue