Tutorials: xargs for beginners, multi-threaded xargs.
find . -name '*.log' -print0 | xargs -0 rm -f # null-separated: safe for any file name
find . -type f -print0 | xargs -0 grep -l 'ERROR'
echo a b c | xargs -n 1 echo # one argument per command
printf '%s\n' host1 host2 | xargs -I{} ssh {} uptime # -I{} substitutes each item
cat urls.txt | xargs -n 1 -P 4 curl -sO # 4 parallel downloads
xargs -a args.txt -d '\n' command # read from a file, newline-delimited
| Option | Meaning |
|---|---|
-0 |
input items are NUL-terminated (pair with find -print0) |
-n N |
at most N arguments per command run |
-P N |
run up to N commands in parallel (-P 0 = as many as possible) |
-I{} |
replace {} in the command with each input line |
-r |
do not run the command if input is empty (GNU) |
-t |
print each command before running it |
Without -0, spaces and quotes in names break things. Preview with xargs -t or xargs echo command. For richer parallel jobs see GNU parallel; find ... -exec cmd {} + often replaces xargs entirely.