このチュートリアルでは、指定された深度範囲内でファイルとディレクトリを検索するLinux find
コマンドを使用して、 maxdepth
およびmindepth
オプションを使用して示しています。簡単な類推は、初心者向けのこれらのオプションを明確にするのに役立ちます。
maxdepth
とmindepth
理解する
maxdepth
オプションは、検索をディレクトリ階層内の最大数のレベルに制限します。 mindepth
、検索が開始される最小深度レベルを設定します。これらのオプションは、特定のディレクトリツリーレベルに焦点を当てることにより、検索を改善します。
実用的な例
このディレクトリ構造をデモンストレーションに使用しましょう。
<code>MyFiles/ ├── Dir1 │ ├── Dir2 │ │ ├── Dir3 │ │ │ └── file3 │ │ └── file2 │ ├── file1 │ └── file1.1 ├── myfile1 ├── myfile2 └── myfile3</code>
maxdepth
例:
find MyFiles/ -maxdepth 1
: MyFiles/
(dir1、myfile1、myfile2、myfile3)の直接のすべてを、より深いレベルを除くすべてをリストします。find MyFiles/ -maxdepth 2
:dir1、myfile1、myfile2、myfile3、およびdir1(dir2、file1、file1.1)の内容は含まれますが、dir3またはその内容は含まれません。 mindepth
例:
find MyFiles/ -mindepth 2
: MyFiles/
dir2、file1、file1.1などのすべてをリストします。find MyFiles/ -mindepth 3 -type f
:3以上の深さ(file2、file3)のファイルのみをリストします。 maxdepth
とmindepth
を組み合わせた:
find MyFiles/ -mindepth 3 -maxdepth 3 -type f
:Filesを正確に3レベルにリストします(File2)。特定のファイル/ディレクトリの検索:
find /path/to/directory/ -maxdepth 2 -name file1
find /path/to/directory/ -maxdepth 3 -name file2
find /path/to/directory/ -mindepth 2 -maxdepth 4 -name file
find /path/to/directory/ -name file3
アナロジー:森の探索
ディレクトリレベルを森のレイヤーと考えてください。
mindepth
:検索を開始する前に探索する最小の深さ(「2回目のクリアを超えてだけ探索する」)。maxdepth
:探索する最大深度(「3回目のクリアリングよりも深くはなりません」)。結論
maxdepth
とmindepth
、 find
コマンドの検索スコープをきめぶし制御し、複雑なディレクトリ構造を扱うときに効率を向上させます。 maxdepth
上限を設定し、 mindepth
検索の出発点を定義することを忘れないでください。
以上がlinux findコマンドでmaxdepthおよびmindepthオプションの使用方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。