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

Charlie avatar
By Charlie
at 2011-03-18T08:23

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 次

--
Tags: Linux

All Comments

Iris avatar
By Iris
at 2011-03-23T04:38
Mason avatar
By Mason
at 2011-03-27T09:35
但這會有時遇到引數過長,cmd拒絕接受的情形
Cara avatar
By Cara
at 2011-03-31T06:17
感謝!原來如此,所以其實find就有說明了,我一直找bash

platform device driver的問題

Callum avatar
By Callum
at 2011-03-18T00:26
※ [本文轉錄自 LinuxDev 看板 #1DWZNWAY ] 作者: poloegg (ZZZzzz) 看板: LinuxDev 標題: Re: [問題] platform device driver的問題 時間: Fri Mar 18 00:25:01 2011 ※ 引述《lunatich ()》之 ...

bash 的 + operator 為什麼省資源

Elvira avatar
By Elvira
at 2011-03-18T00:15
http://davidwalsh.name/delete-svn 剛剛在這篇文章看到 find . -type d -name .svn -exec rm -rf {} \; 很多人都會用來清理.svn資料夾的實用指令。 然後底下第一個回應說: IMHO its better to use find ...

ubuntu開網站問題

Oliver avatar
By Oliver
at 2011-03-17T22:57
我這台ubuntu 不能開土豆網的影片來看 而同一間的電腦,就能開來看,那台我也灌了linux,不過是 ezgo8,也是不能看土豆網 的影片,為什麼這樣?是哪的問題? -- 「那」跟「哪」有那麼難嗎?難道「哪」消失了嗎? 你去那裡 跟 你去哪裡 意思差很多說 - ...

mpich執行port問題

Charlotte avatar
By Charlotte
at 2011-03-17T21:35
使用的OS是ubuntu 10 電腦是四核心的PC 似乎因為ssh server設定不是預設的port 22 造成mpirun.mpich -np 2 .... 執行時出錯 以下是錯誤訊息 ssh: connect to host localhost port 22: Connection refused p ...

ubuntu抓不到內建讀卡機

Oliver avatar
By Oliver
at 2011-03-17T20:18
UBUNTU版本:10.10 我用acer小筆電,內建讀卡機一直抓不到, 如果外接USB讀卡機,就能用。 進去bois看過,小筆電的bois有點簡略,似乎沒有什麼開啟選項。 請問有高手知道怎麼解決嗎? - ...