xargs
xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Commands such as grep and awk can accept the standard input as a parameter, or argument by using a pipe. However, others such as cp and echo disregard the standard input stream and rely solely on the arguments found after the command.[1]
Documentation
- man 1 'xargs' [EN]
Syntax
xargs [PARAMETER ...] [COMMAND [PARAMETER ...]]
Parameters
- -0, --null
- Input file names are terminated by a null character instead of by whitespace, and any quotes and backslash characters are not considered special (every character is taken literally). Disables the end of file string, which is treated like any other argument.
- -I STRING, --replace[=STRING]
- Replace occurrences of STRING in the initial arguments with names read from standard input. Also, unquoted blanks do not terminate arguments; instead, the input is split at newlines only. If STRING is omitted (omitting it is not allowed for -I), it defaults to {} (like for find -exec). Implies -x and -L 1.
- -L NUMBER, --max-lines[=NUMBER]
- Use at most NUMBER non-blank input lines per command line. For --max-lines, NUMBER defaults to 1 if omitted. For -L, the argument is mandatory. Trailing blanks cause an input line to be logically continued on the next input line, for the purpose of counting the lines. Implies -x.
- -n NUMBER, --max-args=NUMBER
- Use at most NUMBER arguments per command line. Fewer than NUMBER arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
- -P NUMBER, --max-procs=NUMBER
- Run simultaneously up to NUMBER processes at once; the default is 1. If NUMBER is 0, xargs will run as many processes as possible simultaneously.
Examples
- Compile all AppleScript files in the current directory and its subdirectories
find '.' -iname '*.applescript' -print0 \
| sed --null-data --quiet 'h; s/applescript$/scpt/I; p; g; p' \
| xargs --max-lines=2 --max-procs=5 --null osacompile -x -o
- Print one word per line[2]
brew cask list | xargs --max-args=1