Posted at

zsh ユーザーなら zargs を使う。

More than 1 year has passed since last update.

xargs の zsh 版みたいなやつ。


先ずは


zsh

autoload -U zargs



基本的な使い方


zsh

zargs -- **/*(.) -- ls


こんな感じで zsh の修飾子や拡張 glob を知っていると、すごく便利に使える。

verbose オプション -t をつけて、max-args を 3 -n3 とかにするとこんな感じ。


zsh

% zargs -t -n3 -- {1..10} -- echo                                                                                  

echo
1 2 3
1 2 3
echo 4 5 6
4 5 6
echo 7 8 9
7 8 9
echo 10
10

-P オプションとかで並列実行するとかもできるし、引数を渡す位置を変更したければ、 -i オプションを用いる。


zsh

% zargs -P0 -i{} -- **/*.jpg -- command -i {} -o ./output



help

% zargs --help                                                                                                                                                                                                    Usage: zargs [options --] [input-args] [-- command [initial-args]]

If command and initial-args are omitted, "print -r --" is used.

Options:
--eof[=eof-str], -e[eof-str]
Change the end-of-input-args string from "--" to eof-str. If
given as --eof=, an empty argument is the end; as --eof or -e,
with no (or an empty) eof-str, all arguments are input-args.
--exit, -x
Exit if the size (see --max-chars) is exceeded.
--help
Print this summary and exit.
--interactive, -p
Prompt before executing each command line.
--max-args=max-args, -n max-args
Use at most max-args arguments per command line.
--max-chars=max-chars, -s max-chars
Use at most max-chars characters per command line.
--max-lines[=max-lines], -l[max-lines]
Use at most max-lines of the input-args per command line.
This option is misnamed for xargs compatibility.
--max-procs=max-procs, -P max-procs
Run up to max-procs command lines in the background at once.
--no-run-if-empty, -r
Do nothing if there are no input arguments before the eof-str.
--null, -0
Split each input-arg at null bytes, for xargs compatibility.
--replace[=replace-str], -i[replace-str]
Substitute replace-str in the initial-args by each initial-arg.
Implies --exit --max-lines=1.
--verbose, -t
Print each command line to stderr before executing it.
--version
Print the version number of zargs and exit.