xargs
is great.You should care about xargs
if you believe what I write
about command line tools in general, because xargs
turns
all of those tools into n-dimensional swiss army knives.
Briefly: xargs
takes a command that you want to run as
its argument and reads the inputs (arguments) to that command from STDIN
.
It’s remarkably powerful when combined with any other tool that writes in a predictable way to stdout.
Here are some invocations that I like:
git grep
--ignore-case --files-with-matches searchterm | xargs -L 1 sed -i
'' 's/searchTerm/replacementTerm/g'
1find . | xargs wc -l | sort -nr
(combine with
awk
to get sum of lines of code)xargs -p
to verify
the command before each invocation. (It’s often nice to see what you’re
doing before you’ve done it, especially if the operation could be
risky)..js
files to .ts
files,
with a prompt for sanity checking: find . -iname '*.js' | grep -E
-i --invert-match 'node_modules|bundle' | xargs -L 1 rename
's/\.js$/\.ts/'
The -L 1
argument to
xargs
means “run this command once per line.” Without this,
commands are executed once per separator (e.g. space or line) found in
the input.↩︎