それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

find execの使い方には2つある「; 」と「+ 」

find の man 見てて次のような記述をみつけた

find コマンドの exec オプションには、「;」 以外に 「+」を使うことが出来た

いか man からの抜粋

-exec command ;
 Execute  command;  true  if  0 status is returned.
  All following arguments to find are taken to be arguments to
 the command until an argument consisting of `;' is encountered.
  The string `{}' is replaced by the current file name being processed 
everywhere it occurs in the arguments to the command, not just in
 arguments where it is alone, as in some versions of find.   Both of  
these  constructions might need to be escaped (with a `\') or 
quoted to protect them from expansion by the shell.
  See the EXAMPLES section for examples of the use of 
the -exec option. The specified command is run once for 
each matched file.  The command is executed in the starting directory. 
  There are unavoidable security problems surrounding use of the -exec  action;

you should use the -execdir option instead.

-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.

man によると、find のexecはシェル経由じゃなく、直接起動するので早いらしい。

-execオプションで、” + “がterminatorの場合

>find /usr/local/ -exec ls -l {} +

参考資料 ⇛ http://subtech.g.hatena.ne.jp/otsune/20100410/findexecplus

-execオプションで、” ; “がterminatorの場合

>find /usr/local/ -exec ls -l {} \;

どちらも同じように起動するわけじゃない。

exec に引数をまとめて渡すか、1つずつ渡すかで差がある。

-exec command ; オプションの例

takuya@air:~/Desktop$ find /usr/local/ -type d  -exec du -cksh {} +
1.5G     /usr/local/
1.5G     total
156K     /usr/local/Cellar/mysql/5.5.24/mysql-test/std_data/ndb_backup51_data_be
156K     /usr/local/Cellar/mysql/5.5.24/mysql-test/std_data/ndb_backup51_data_le
92K     /usr/local/Cellar/mysql/5.5.24/mysql-test/std_data/parts
93M     /usr/local/Cellar/mysql/5.5.24/mysql-test/suite
8.3M     /usr/local/Cellar/mysql/5.5.24/mysql-test/t
16K     /usr/local/Cellar/mysql/5.5.24/scripts
16M     /usr/local/Cellar/mysql/5.5.24/share
2.8M     /usr/local/Cellar/mysql/5.5.24/sql-bench
96K     /usr/local/Cellar/mysql/5.5.24/support-files
(略
346M     total

-exec + と -exec ; の違い

違いは、こんな感じ

+ をつけると、検索結果を全部一行にし、一度に引数を渡す
; をつけると、検索結果のそれぞれに対し、都度都度起動する。

次のような引数展開するスクリプトを使って実験します。

#!/usr/bin/env ruby 
require 'pp'pp ARGV

起動時の引数を表示するようになる簡単なRubyスクリプト

takuya@air:~/Desktop$ ./arg.rb first second third
["first", "second", "third"]

これをfind 経由で使ってみる

「;」を使った場合

引数が一度に渡されているので、ruby はすべてを引数配列にぶち込んでいるのがわかる。

takuya@air:~/Desktop$ find /etc/apache2/ -type f -exec ./arg.rb {} +
["/etc/apache2/extra/httpd-autoindex.conf",
 "/etc/apache2/extra/httpd-dav.conf",
 "/etc/apache2/extra/httpd-default.conf",
 "/etc/apache2/extra/httpd-info.conf",
 "/etc/apache2/extra/httpd-languages.conf",
 "/etc/apache2/extra/httpd-manual.conf",
 "/etc/apache2/extra/httpd-mpm.conf",
 "/etc/apache2/extra/httpd-multilang-errordoc.conf",
 "/etc/apache2/extra/httpd-rewrite.conf",
 "/etc/apache2/extra/httpd-ssl.conf",
 "/etc/apache2/extra/httpd-userdir.conf",
 "/etc/apache2/extra/httpd-vhosts.conf",
 "/etc/apache2/httpd.conf",
 "/etc/apache2/httpd.conf~previous",
 "/etc/apache2/magic",
 "/etc/apache2/mime.types",
 "/etc/apache2/original/extra/httpd-autoindex.conf",
 "/etc/apache2/original/extra/httpd-dav.conf",
 "/etc/apache2/original/extra/httpd-default.conf",
 "/etc/apache2/original/extra/httpd-info.conf",
 "/etc/apache2/original/extra/httpd-languages.conf",
 "/etc/apache2/original/extra/httpd-manual.conf",
 "/etc/apache2/original/extra/httpd-mpm.conf",
 "/etc/apache2/original/extra/httpd-multilang-errordoc.conf",
 "/etc/apache2/original/extra/httpd-ssl.conf",
 "/etc/apache2/original/extra/httpd-userdir.conf",
 "/etc/apache2/original/extra/httpd-vhosts.conf",
 "/etc/apache2/original/httpd.conf",
 "/etc/apache2/other/php5.conf",
 "/etc/apache2/users/Guest.conf",
 "/etc/apache2/users/takuya.conf"]

「;」を使った場合

ruby が何度も起動している。引数1つに、ruby が一回起動する。

つまり、見つかった分だけ起動している

takuya@air:~/Desktop$ find /etc/apache2/ -type f -exec ./arg.rb {} \;
["/etc/apache2/extra/httpd-autoindex.conf"]
["/etc/apache2/extra/httpd-dav.conf"]
["/etc/apache2/extra/httpd-default.conf"]
["/etc/apache2/extra/httpd-info.conf"]
["/etc/apache2/extra/httpd-languages.conf"]
["/etc/apache2/extra/httpd-manual.conf"]
["/etc/apache2/extra/httpd-mpm.conf"]
["/etc/apache2/extra/httpd-multilang-errordoc.conf"]
["/etc/apache2/extra/httpd-rewrite.conf"]
["/etc/apache2/extra/httpd-ssl.conf"]
["/etc/apache2/extra/httpd-userdir.conf"]
["/etc/apache2/extra/httpd-vhosts.conf"]
["/etc/apache2/httpd.conf"]
["/etc/apache2/httpd.conf~previous"]
["/etc/apache2/magic"]
["/etc/apache2/mime.types"]
["/etc/apache2/original/extra/httpd-autoindex.conf"]
["/etc/apache2/original/extra/httpd-dav.conf"]
["/etc/apache2/original/extra/httpd-default.conf"]
["/etc/apache2/original/extra/httpd-info.conf"]
["/etc/apache2/original/extra/httpd-languages.conf"]
["/etc/apache2/original/extra/httpd-manual.conf"]
["/etc/apache2/original/extra/httpd-mpm.conf"]
["/etc/apache2/original/extra/httpd-multilang-errordoc.conf"]
["/etc/apache2/original/extra/httpd-ssl.conf"]
["/etc/apache2/original/extra/httpd-userdir.conf"]
["/etc/apache2/original/extra/httpd-vhosts.conf"]
["/etc/apache2/original/httpd.conf"]
["/etc/apache2/other/php5.conf"]
["/etc/apache2/users/Guest.conf"]
["/etc/apache2/users/takuya.conf"]

ね?複数回起動しているのが分かるよね。

模式的にRubyプログラミングで書いてみます。

これが +

exec "#{cmd} #{Dir.glob("/etc/apache2").select{|e| File.file? e}.join(' ')}"

これが ; 

Dir.glob("/etc/apache2").select{|e| File.file? e}.each{|e|  exec "#{cmd} #{e}"}

随分違いますね。