pipeline
xargs
Builds and runs command lines from standard input. Bridges producers like find or grep to per-item actions efficiently.
xargs [OPTION]... [COMMAND [ARG]...] Common flags
| Flag | Purpose |
|---|---|
| -0 | Expect NUL-separated input (pair with find -print0) |
| -n | Pass at most N arguments per command invocation |
| -I | Replace occurrences of REPLACE-STR (e.g. -I {}) in the command |
| -P | Run up to N invocations in parallel |
| -r | Do not run the command if input is empty |
| -t | Print each command before running it |
Examples
find . -name '*.tmp' -print0 | xargs -0 rm Delete temp files safely with NUL separators
cat urls.txt | xargs -n1 -P4 curl -sSO Download URLs four at a time
ls *.jpg | xargs -I{} cp {} backup/{} Copy each match into a backup folder
grep -rl 'debug' src | xargs sed -i 's/debug/info/g' Replace text across matched files
Gotcha
Without -0, xargs splits on whitespace and quotes, so filenames with spaces break -- always feed it -print0 or use --null.
Related commands
find
Recursively walks directories evaluating expressions on each entry. Combines discovery with actions like exec or delete.
grep
Searches text for lines matching a regular expression. Central to log analysis and pipeline filtering.
sed
Stream editor that applies a small program to each line of input. Widely used for search-and-replace and inline file edits.