Skip to content
Latchkey

xargs Command Reference for CI Scripts

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.

Common flags/usage

  • -0 / --null: input is null-delimited (pairs with find -print0)
  • -n N: pass at most N arguments per command
  • -P N: run up to N commands in parallel
  • -I {}: replace {} with each item (implies -n1)
  • -r / --no-run-if-empty: do nothing on empty input (GNU)

Example

shell
git diff --name-only --diff-filter=ACM | grep '\.ts$' \
  | xargs -r prettier --write
cat urls.txt | xargs -P4 -I{} curl -fsS -o /dev/null {}

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. Always use -r, or guard the pipeline. Filenames with spaces break default splitting; use find -print0 | xargs -0. xargs also solves "argument list too long".

Key takeaways

  • Use -r so xargs does nothing on empty input instead of running once with no args.
  • -P runs commands in parallel; -I {} substitutes each item into a template.
  • Combine -0 with find -print0 to safely handle odd filenames.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →