bash 的 + operator 為什麼省資源 - Linux

Table of Contents

※ 引述《WandererM (WM)》之銘言:
: http://davidwalsh.name/delete-svn
: 剛剛在這篇文章看到
: find . -type d -name .svn -exec rm -rf {} \;
: 很多人都會用來清理.svn資料夾的實用指令。
: 然後底下第一個回應說:
: IMHO its better to use
: find . -type d -name .svn -exec rm -rf {} +
: since it saves system resources. And IFAIK all todays bash support + operator.
: 請問有人可以解釋一下+號在這裡的用法是怎麼回事?為什麼比較省資源?
: 如果有bash對這個的官方說明就更好了,感謝!

find 的 manual 是這樣寫的:

-exec command {} +
This variant of the -exec action runs the specified command on the
selected files, but the command line is built by appending each selected
file name at the end; the total number of invocations of the command will
be much less than the number of matched files. The command line is built
in much the same way that xargs builds its command lines. Only one instance
of `{}' is allowed within the command. The command is executed in the
starting directory.

意思是說,用 -exec cmd {} + 的話,他會把符合的檔名接在一起,

所以 cmd 被執行的真正次數會比較少

Ex:

$ find . -type f -regex '.*\.c' -exec echo {} \; | wc -l
==> 98
$ find . -type f -regex '.*\.c' -exec echo {} + | wc -l
==> 1

可以看到用 -exec cmd {} + 的話,只呼叫了一次 echo ,

而 -exec cmd {} \; 呼叫了 98 次

--

All Comments

Iris avatarIris2011-03-23
Mason avatarMason2011-03-27
但這會有時遇到引數過長,cmd拒絕接受的情形
Cara avatarCara2011-03-31
感謝!原來如此,所以其實find就有說明了,我一直找bash