xargs Basics: Build Commands From stdin
xargs reads whitespace-separated items from stdin and appends them as arguments to a command you give it.
xargs turns a list of items into one or more command invocations. The canonical use is find piped into xargs to act on the files it found.
What it does
xargs reads items from standard input, splits them on whitespace and newlines, and runs the command you name once with as many items as fit on a command line. If you give no command, it defaults to running echo. It packs many items into each invocation rather than one run per item.
Common usage
find . -name '*.log' | xargs rm
echo "a b c" | xargs # echoes: a b c
find . -name '*.js' | xargs wc -lOptions
| Flag | What it does |
|---|---|
| (no command) | Defaults to running /bin/echo with the items |
| command [args] | Run this command, appending the items as extra arguments |
| -0 / --null | Read NUL-separated items (pair with find -print0) |
| -n <max> | Use at most max items per command run |
| -I <repl> | Replace repl in the command with one item per run |
Common errors in CI
A pipeline that breaks on filenames containing spaces is the classic trap: plain "find | xargs" splits "my file.log" into two arguments. The fix is find -print0 piped to xargs -0. If the command itself is missing you get "xargs: <command>: No such file or directory" or "Permission denied" from xargs, not from your shell.