LoginSignup
28
26

More than 5 years have passed since last update.

findコマンドで複数拡張子を指定して、コマンドを実行する

Posted at

findコマンドで複数の拡張子を指定して、見つけたファイルに対してコマンドを実行する方法は以下のとおりです。

find 検索対象ディレクトリ \( 検索条件 \) -exec コマンド

以下の例では、検索対象ディレクトリ内のCソース、ヘッダファイル、アセンブラソースを出力し、その全ステップ数を数えます。
(検索対象ディレクトリは複数指定可能です。)

find src/ include/ \( -name \*.c -or -name \*.h -or -name \*.S \) -exec cat {} \; | wc -l

上記で注意すべき点は以下のとおりです。
1.拡張子の検索条件は括弧でくくっていますが、エスケープしましょう。
2.括弧と検索条件を示すオプション(ここでは-name)との間にはスペースを入れる。入れないとfindコマンドに以下のように怒られます。

find: invalid expression; you have used a binary operator '-or' with nothing before it.

これだけ見てすぐにピンとこないので、余計な時間を取らないためにも「括弧ときたらスペース」と心得ましょう。

28
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
26