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

By Charlie
at 2011-03-18T08:23
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 次
--
: 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

By Iris
at 2011-03-23T04:38
at 2011-03-23T04:38

By Mason
at 2011-03-27T09:35
at 2011-03-27T09:35

By Cara
at 2011-03-31T06:17
at 2011-03-31T06:17
Related Posts
platform device driver的問題

By Callum
at 2011-03-18T00:26
at 2011-03-18T00:26
bash 的 + operator 為什麼省資源

By Elvira
at 2011-03-18T00:15
at 2011-03-18T00:15
ubuntu開網站問題

By Oliver
at 2011-03-17T22:57
at 2011-03-17T22:57
mpich執行port問題

By Charlotte
at 2011-03-17T21:35
at 2011-03-17T21:35
ubuntu抓不到內建讀卡機

By Oliver
at 2011-03-17T20:18
at 2011-03-17T20:18