Frequently Asked Question
What is xargs and when should I use it?
Most Unix commands fall into one of two camps: those that read their input from
stdin (grep, sort, wc) and those that take their input as command-line
arguments (rm, cp, mv, ls). xargs bridges the two: it reads lines from
stdin and re-issues the command on its right with those lines tacked on as
arguments. So find . -name "*.tmp" | xargs rm produces a call like rm file1.tmp file2.tmp file3.tmp ... rather than running rm once per file.
xargs is fast, it batches arguments up to the kernel's ARG_MAX limit, so it
avoids the per-file process startup cost of running find ... -exec rm {} \;.
The big gotcha is whitespace: filenames with spaces or newlines confuse xargs
by default, because it splits on those characters. The safe idiom is find ... -print0 | xargs -0 ..., which uses NUL bytes (the one byte filenames can't
contain) as separators. Modern alternatives like find ... -exec ... + and
GNU parallel handle similar work with different trade-offs.