Skip to content
Latchkey

find -print0: Safe Piping With NUL Delimiters

find -print0 ends each path with a NUL byte instead of a newline so downstream tools split safely.

NUL is the only byte that cannot appear in a path, so -print0 paired with xargs -0 is the only fully safe way to pipe arbitrary file names.

What it does

find -print0 prints each match followed by a NUL (\0) byte rather than a newline. Tools that understand NUL delimiters, chiefly xargs -0 and grep -z, then split on NUL, so spaces, tabs, and newlines inside file names are handled correctly.

Common usage

Terminal
find . -name '*.tmp' -print0 | xargs -0 rm -f
find . -type f -print0 | xargs -0 grep -l TODO
find . -name '*.bak' -print0 | xargs -0 -P4 gzip

Options

ElementWhat it does
-print0Terminate each path with NUL instead of newline
xargs -0Read NUL-delimited input
grep -zTreat input as NUL-delimited records
sort -zSort NUL-delimited records

In CI

Any pipeline that processes find output should use -print0 | xargs -0. It is the difference between a cleanup that works on clean repos and one that breaks the day a file name contains a space. -exec {} + is an equally safe alternative with no pipe.

Common errors in CI

-print0 is a GNU extension but is also implemented by BSD/macOS find, so it is portable in practice. If output looks like one long line, that is expected: NUL bytes are invisible in a terminal. Pairing -print0 with a tool that does not support -0 (plain xargs without -0) produces garbled arguments.

Related guides

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