複数のサブディレクトリで特定のファイルを検索する

複数のサブディレクトリで特定のファイルを検索する

次の質問があります。

Aディレクトリに含まれているファイルをコピーするには、何かを作成する必要があります。ファイルは一意であり、特定の拡張子(.jil)を持ちます。唯一の問題は、ディレクトリAにサブディレクトリB(サブディレクトリCなどを含めることができます)を含めることができ、それで私のファイルがディレクトリAではなくそのサブディレクトリの1つにあることです。

すべてのディレクトリを参照せずにこのファイルを見つけるには、どのコマンドを使用できますか?

ベストアンサー1

findこれが実際に基本的な作業方法です。君はただ走れるよ

find A/ -name "*.jil" -exec cp {} target_dir/ \;

デフォルトの動作は、findすべてのサブディレクトリを再帰的に表示することです。これは設定-maxdepth/-midepthオプションで制御できます。

   -maxdepth levels
          Descend at most levels (a non-negative integer) levels of direc‐
          tories below the command line arguments.  -maxdepth 0
           means only apply the tests and  actions  to  the  command  line
          arguments.
   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

おすすめ記事