Re: [問題] bash 的 + operator 為什麼省資源

看板Linux作者 (qqaa)時間15年前 (2011/03/18 08:23), 編輯推噓3(300)
留言3則, 3人參與, 最新討論串2/2 (看更多)
※ 引述《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 次 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.182

03/18 09:16, , 1F
03/18 09:16, 1F

03/18 11:05, , 2F
但這會有時遇到引數過長,cmd拒絕接受的情形
03/18 11:05, 2F

03/18 19:28, , 3F
感謝!原來如此,所以其實find就有說明了,我一直找bash
03/18 19:28, 3F
文章代碼(AID): #1DWgO9R3 (Linux)
文章代碼(AID): #1DWgO9R3 (Linux)