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
find Command Reference for CI Scriptsfind walks directory trees and acts on matching files in CI. Reference for -name, -type, -exec, and -print0,…
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
rm -rf Command Reference for CI Scriptsrm -rf removes files and directories in CI cleanup. Reference for -r, -f, and safe-deletion habits, plus the…
rsync Command Reference for CI Scriptsrsync synchronizes files efficiently in CI deploys and cache restores. Reference for -a, -z, --delete, and th…