Skip to content
Latchkey

xargs -0: Safe Filenames With find -print0

xargs -0 splits input on NUL bytes instead of whitespace, so spaces and newlines in filenames are handled safely.

NUL is the one byte that cannot appear in a filename, so -0 is the only fully safe way to feed file lists into xargs. Always pair it with find -print0.

What it does

xargs -0 (long form --null) treats the NUL byte as the item separator and stops treating whitespace, quotes, and backslashes as special. Combined with find -print0, which emits NUL-terminated paths, it passes every filename through intact regardless of spaces, tabs, or newlines.

Common usage

Terminal
find . -name '*.tmp' -print0 | xargs -0 rm
find . -type f -print0 | xargs -0 grep -l TODO
# GNU only: split a NUL-delimited list from a file
xargs -0 -a files.nul rm

Options

FlagWhat it does
-0Read items separated by NUL bytes
--nullGNU long form of -0
(with find -print0)find emits NUL-terminated paths to match
(with grep -Z)grep -lZ emits NUL-terminated matching filenames

In CI

Make -print0 plus xargs -0 the default for any file operation in a pipeline. Repos checked out in CI routinely contain paths with spaces (fixtures, snapshots), and the whitespace bug shows up only on those inputs, so it slips through local testing and fails in the runner.

Common errors in CI

Without -0, a path like "test cases/a.txt" becomes two arguments and you see "rm: cannot remove 'test': No such file or directory". If you pass -0 but feed plain newline output (no -print0), the whole list arrives as one giant item and you get "argument line too long" or a single bogus filename. Match -print0 with -0 on both sides.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →