xargs
The xargs
command in UNIX is a command line utility for building an execution pipeline from standard input. Whilst tools like grep
can accept standard input as a parameter, many other tools cannot. Using xargs
allows tools like echo
and rm
and mkdir
to accept standard input as arguments.
1. How to use xargs
By default xargs
reads items from standard input as separated by blanks and executes a command once for each argument. In the following example standard input is piped to xargs and the mkdir
command is run for each argument, creating three folders.
1 | echo 'one two three' | xargs mkdir |
2. xargs
v exec
{}
1 | find ./foo -type f -name "*.txt" -exec rm {} \; |