tips
12 tips Xargs
One command, setting or habit at a time about Xargs: what it does, the line to copy and a caveat or two.
Linux and shell 12
-
Sets an environment variable in each parallel xargs job, holding the slot number it runs in.
printf '%s\n' a b c d e f | xargs -n1 -P3 --process-slot-var=SLOT sh -c 'echo "slot $SLOT got $0"; sleep 0.2' -
Connects the command xargs starts to your terminal, so editors and prompts work normally.
grep -l 'TODO' *.py | xargs -o vim -
Shows every command xargs is about to run and waits for you to confirm it.
find . -name '*.orig' -print0 | xargs -0 -p rm -
Explains xargs exit codes, so a script can tell whether every command it ran succeeded.
find . -name '*.gz' -print0 | xargs -0 -n 1 gzip -t -
Keeps each input line together as the arguments of one command run.
printf 'a b\nc d\n' | xargs -L 1 echo line: -
Shows the exact commands xargs builds, which makes a pipeline easy to check and debug.
printf 'x\ny\n' | xargs -t touch -
Prints the system's limit on command length and the buffer size xargs really uses.
xargs --show-limits < /dev/null -
Splits a list into fixed-size groups, so each command run gets exactly the number of arguments it needs.
printf 'old1.txt new1.txt\nold2.txt new2.txt\n' | xargs -n 2 mv -
Takes the items from a file so the command that xargs runs can still use the keyboard.
xargs -a hosts.txt -n 1 ssh-keygen -R -
Places each input line at a chosen spot in the command, not just at the end.
find . -maxdepth 1 -name '*.conf' | xargs -I {} cp {} {}.bak -
Stops xargs from running the command when the pipeline before it produced no input.
docker ps -aq --filter status=exited | xargs -r docker rm -
Runs a command on several inputs at the same time instead of one after another.
find . -name '*.png' -print0 | xargs -0 -n 1 -P 4 optipng
Nothing in this filter yet.