Skip to content
Latchkey

find | xargs: Pipe Matches to a Command

find piped to xargs feeds matched paths as arguments to another command in batches.

A classic shell idiom for running a command over many files. It works, but plain find | xargs breaks on spaces, so know the safe form.

What it does

find ... | xargs CMD prints matched paths, and xargs reads them and runs CMD with them as arguments in batches. By default xargs splits on whitespace and newlines, which corrupts file names that contain spaces unless you use the null-delimited form.

Common usage

Terminal
find . -name '*.tmp' | xargs rm -f
# safe with spaces in names:
find . -name '*.tmp' -print0 | xargs -0 rm -f
find . -name '*.log' | xargs grep -l ERROR

Options

ElementWhat it does
xargs CMDRun CMD with the piped items as arguments
-0Read NUL-delimited input (pair with find -print0)
-I {}Replace {} per item (forces one run per item)
-n NAt most N arguments per command invocation
-P NRun up to N invocations in parallel

In CI

Plain find | xargs splits on spaces and newlines, so a path like "my file.txt" becomes two arguments and the command fails or deletes the wrong thing. Always use find -print0 | xargs -0, or prefer find -exec CMD {} + which has no quoting pitfalls.

Common errors in CI

"xargs: unmatched single quote" or "No such file or directory" on a path with spaces means you skipped -print0 / -0. An empty pipe makes some xargs run the command once with no arguments; pass --no-run-if-empty (GNU) or -r to suppress that. BSD/macOS xargs uses -o not -r differently, so prefer -print0 | xargs -0.

Related guides

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