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.
df -P /workspace
df -h /workspaceThe 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.
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.
du -sh /usr/bin
du -sb --apparent-size /workspaceThe 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.
du -s /usr/* 2>/dev/null | sort -n | tail -3The 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.
mkdir -p data
head -c 1048576 /dev/zero > data/one.bin
head -c 2048 /dev/zero > data/two.bin
du -sb --apparent-size dataThe 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.