Measure selected files with a NUL-delimited du manifest
A filename can contain a space or newline. Passing a selected file list to GNU du through NUL-delimited records avoids splitting those names into separate arguments.
Run the example
Tested on Linux with GNU Bash 5.2.37, coreutils 9.7, grep 3.11 and diffutils 3.10. Use one Bash session and a text editor. Create a disposable working directory before saving the source:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Save as demo_linux_du_null_file_manifest.sh:
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
cleanup() {
rm -f -- "$fixture_dir/two words" "$fixture_dir/"$'line\nbreak'
rmdir -- "$fixture_dir"
}
trap cleanup EXIT
cd -- "$fixture_dir"
printf abc > 'two words'
printf 12345 > $'line\nbreak'
printf '%s\0' 'two words' $'line\nbreak' |
du --apparent-size --block-size=1 --null --files0-from=- |
while IFS= read -r -d '' record; do
printf '%q\n' "$record"
doneRun it:
bash demo_linux_du_null_file_manifest.shObserved output:
$'3\ttwo words'
$'5\tline\nbreak'The input manifest and du output both use NUL terminators. The Bash read loop keeps each record intact, and %q displays control characters as shell escapes so the embedded newline is visible. --apparent-size --block-size=1 reports logical byte lengths, making the two fixture results independent of filesystem allocation units.
Limits and input policy
This measures logical size, not reserved disk space or a storage bill. Hard links are normally counted once; directories cause recursive traversal. The fixture lists two regular files only. A real producer must emit NUL delimiters directly, and every later consumer must preserve that framing. Do not replace it with command substitution, which cannot retain NUL bytes.
Clean up
Remove the saved script and the empty working directory in the same Bash session:
rm -- demo_linux_du_null_file_manifest.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect its contents before removing anything else.
Reference
Acknowledgements
This post was created with assistance from AI.