Linux Basics: Compare Apparent and Allocated File Size with GNU du
A downloaded image or sparse database file can have a large logical length while occupying much less storage. GNU du can report both measurements, but changing the display unit alone does not switch between them.
Prepare a scratch directory
Tested on Linux with GNU coreutils 9.7, GNU findutils 4.10.0, and GNU Bash 5.2.37. You need Bash, a text editor, and the utilities used below. The script creates its own temporary fixture directory and removes only that directory on exit.
Keep the same terminal open. Stop if setup fails, before saving the source:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save this as demonstrate-gnu-du-sparse-files.sh:
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
truncate -s 1048576 sparse.bin
printf 'Apparent bytes:\n'
du --apparent-size --block-size=1 sparse.bin
printf 'Allocated bytes reported by the filesystem:\n'
du --block-size=1 sparse.bin
printf 'Logical length from stat:\n'
stat --format=%s sparse.bin
test "$(stat --format=%s sparse.bin)" -eq 1048576Run it:
bash demonstrate-gnu-du-sparse-files.shObserved output:
Apparent bytes:
1048576 sparse.bin
Allocated bytes reported by the filesystem:
0 sparse.bin
Logical length from stat:
1048576Read the result
truncate extends a new file to one mebibyte without writing a mebibyte of data. The unfilled region reads as zero bytes. --apparent-size selects logical length, and --block-size=1 displays the selected measurement in bytes. Without --apparent-size, du reports filesystem usage. The final stat check independently verifies the logical length.
Apply it carefully
The allocated value shown here is an observation from this test filesystem, not an assertion every filesystem must satisfy. Allocation units, compression, and filesystem implementation can change it. GNU du -b means apparent bytes; it is not shorthand for allocated bytes. A directory total also has hard-link accounting and traversal rules, so this single-file example does not predict the space a recursive copy will consume. Copying a sparse file without preserving holes can require far more storage.
The GNU reference documents these options and their behavior.
Clean up
Remove the saved script, return to the starting directory, and remove the empty practice directory:
rm -- demonstrate-gnu-du-sparse-files.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect what remains rather than deleting unknown files.
Acknowledgements
This post was created with assistance from AI.