Skip to content
Latchkey

xargs: Usage, Options & Common CI Errors

xargs turns lines of stdin into arguments for a command you choose.

xargs is how you batch and parallelize work over a list of files. Its sharpest edge in CI is empty input: by default it still runs the command once, which can delete or process the wrong thing.

What it does

xargs reads items from stdin (whitespace- or newline-delimited) and appends them as arguments to a command, splitting into multiple invocations to stay under the OS argument-length limit.

Common usage

Terminal
find . -name '*.o' | xargs rm
find . -print0 | xargs -0 rm           # null-safe for odd names
echo "a b c" | xargs -n1 echo          # one arg per call
cat urls.txt | xargs -P4 -I{} curl -fsS {}   # 4 in parallel
git diff --name-only | xargs -r prettier --write   # skip if empty

Options

FlagWhat it does
-0 / --nullInput is null-delimited (pairs with find -print0)
-n <N>Pass at most N args per command
-P <N>Run up to N commands in parallel
-I {}Replace {} with each item (implies -n1)
-r / --no-run-if-emptyDo nothing on empty input (GNU)

Common errors in CI

Empty input is the trap: GNU xargs without -r still runs the command once with no args, so xargs rm on an empty list errors and xargs docker rmi may target the wrong thing. Always use -r (GNU) or guard the pipeline; note BSD/macOS xargs does not run on empty input and has no -r. Filenames with spaces or newlines break the default splitting - use find -print0 | xargs -0. "argument list too long" is solved by xargs (or -n to chunk).

Related guides

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