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.
xargs takes a command that you want to run as its argument and reads the inputs (arguments) to that command from
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'1
find . | xargs wc -l | sort -nr(combine with
awkto get sum of lines of code)
xargs -pto 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).
.tsfiles, with a prompt for sanity checking:
find . -iname '*.js' | grep -E -i --invert-match 'node_modules|bundle' | xargs -L 1 rename 's/\.js$/\.ts/'
-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.↩︎