Safe with file names
Spaces and newlines in file names break a naive xargs. The right way
is a NUL delimiter:
Turns stdin lines into a command's arguments; without -0, file names with spaces and newlines break the parsing.
find . -name '*.x' -print0 | xargs -0 rm
safe (spaces)
... | xargs -I{} cp {} /dst/
substitute {}
... | xargs -P4 -n1 cmd
in parallel
... | xargs -rt cmd
skip if empty + show it
... | xargs -n1
one argument at a time
xargs takes lines from stdin and passes them as a command's
arguments. Needed when a command doesn't read stdin but expects
arguments instead (like rm, chmod).
Without -0, xargs splits the input on spaces and newlines — a file
name with a space turns into two separate arguments. The safe path is a
NUL delimiter on both sides of the pipeline: find -print0 (or grep
-Z) together with xargs -0. -I {} implicitly limits the call to one
argument at a time, like -n1, because you can't substitute several
values into one placeholder at once.
| Option | What it does | Example |
|---|---|---|
-0 |
Input is NUL-delimited — safe for names with spaces (pairs with find -print0). | find . -name '*.log' -print0 | xargs -0 rm |
-n N |
Pass at most N arguments per invocation of the command. | ... | xargs -n1 cmd |
-I {} |
Substitute the argument anywhere in the command, one at a time. | ... | xargs -I{} cp {} /backup/ |
-r --no-run-if-empty |
Do not run the command at all if there is no input. | ... | xargs -r cmd |
-P N |
Run up to N processes in parallel. | cat urls.txt | xargs -P8 -I{} curl -sO {} |
-t |
Print the assembled command before running it — useful for debugging. | ... | xargs -t cmd |
-a FILE |
Read arguments from a file instead of stdin. | xargs -a urls.txt wget |
Nothing found. Try a shorter query.
Spaces and newlines in file names break a naive xargs. The right way
is a NUL delimiter:
find ... -print0 | xargs -0 (or find -exec cmd {} +, see find).
Add -r (--no-run-if-empty) — don't run the command with no
arguments.
xargs splits into batches itself (a system limit); to control it, use
-n N. An alternative is find -exec cmd {} + (which also groups them).
xargs -I{} cmd ... {} ... — {} is substituted at the given spot (one
call at a time).
With -P, lines from different processes interleave. Log to separate
files, or use GNU parallel (which can order the output).
Found an inaccuracy? Report an error in the article