zsh-builtin xargs
15 Mar 2017Of course all of you already knows about xargs: nice cli-mode tool to execute command lines from standard input. As you know some tools can get input as a parameter like awk or grep, but some like cp or 7z can’t. Here I shall talk about xargs and one of zsh-builtin replacement of it. If you are interested please read further.
Also you cannot to bypass it directly by value as
because of it’s error prone and also can lead to “argument list too long” error. It’s the point where xargs appears, for example you can use something like that:
Also you can handle filenames which are not well defined(multiline, tricky symbols, etc) with that:
Also xargs can be used as easy-to-use “replacement”, as minimum in some cases, to gnu parallel with -P flag use it like that:
8 threads are used here.
All of these features are very nice. But you cannot bypass functions from zsh or another shell to xargs, only commands. Also it’s not perfect because of poor performance when amount of input is large.
Instead zsh has nice zargs addon:
To use is you need to autoload:
and then use it in way like that:
to add arguments to zargs you need to use – from both sides like here:
To prove that it’s better consider following task:
you need(for some reason) to count all usings of word “mutex” in the linux kernel headers:
and for zargs you have:
As you can see zargs also works faster than find + xargs.
Note that if you are using fasd or stuff like that then true performance(time which you need to wait) will be increased significantly and you will notice that, but “time” util is not care about true performance.