« Back to Index

Shell: xarg examples

View original Gist on GitHub

Tags: #xargs #bash #shell

xarg examples.bash

seq 10 | xargs -t -I % -n 2 -p echo "foo % bar % baz % qux % qiz %"

# -t prints what would be executed
# -I lets you choose a character to act as a placeholder for input (commonly {} is used, but % looks nicer and fits programming syntax)
#    otherwise input is _appended_ to the end of the command
# -n works only if -I isnt' provided and controls how many lines of input are passed to the 'utility' to be executed (e.g. the echo command)
#    for example: `seq 5 | xargs -n 2` would print `1 2` on one line, then `3 4` on the next line, then finally `5`
# -p prompts you to confirm if the command should be executed (if set, then you don't also need -t)
#
# Also, the performance of using xargs to remove/rename files is magnitudes faster than `find` with the `-exec` flag!