devutils/ATFQ/book ATFQ git

Part III · Shell and commands

28.5 xargs

alsoxargsarguments from stdinargument list too long

TL;DR

  • xargs turns its input into arguments for a command, many at a time.
  • Use -0 with find -print0: names with spaces or newlines stay whole.
  • -n 1 runs the command once per item; -P 4 runs four at a time.
find . -name '*.log' -print0 | xargs -0 gzip
xargs -n 1 -P 4 curl -sO < urls.txt

Pitfalls

Deep dive 2 questions

Why not $(…)?

⚠ rm $(find . -name '*.tmp') also splits names on spaces. And with thousands of files, the command fails with "Argument list too long": the kernel caps the total size of a command's arguments. xargs splits the list into as many runs as needed, each one under the limit.

xargs or find -exec?

find -exec cmd {} + batches arguments the same way, without a pipe (see 28.1). xargs is for input from anywhere (a file, grep -l, a list of URLs) and for running several commands at once with -P.

Commands
xargs
See also
28.1 find