Xargs

如何使用 xargs -I 減少執行次數

  • May 18, 2017

find命令有兩種方式來執行某種xargs風格的東西。

find foo -exec bar '{}' baz ';'
find foo -exec bar '{}' baz '+'

它們之間的區別在於,第一個將每個找到的文件執行一次,第二個將對文件進行分組以減少執行次數。

當我看它時,xargs它有-I開關,例如:

xargs -I{} foo bar {} baz

這將每個 arg 執行一次。

問題:xargs有 沒有辦法減少執行次數find

我想做這樣的事情:

xargs -I{} echo start {} end << EOF
hello
world
EOF

並得到結果:

start hello world end

不是:

start hello end
start world end

我認為這樣做的唯一方法是為您的命令製作一個簡單的包裝腳本:

#!/bin/sh
echo start "$@" end

那麼你可以使用xargs echo-wrapper

當然,您可以內聯執行此操作:

xargs sh -c 'echo start "$@" end' sh <<EOF
hello 
world
EOF

請注意最後的額外內容——這是語法sh的一部分,它在 shell 內部指定。sh -c``$0

引用自:https://unix.stackexchange.com/questions/365881