max深度値が2の場合でも、findコマンドはサブフォルダを送信します。

max深度値が2の場合でも、findコマンドはサブフォルダを送信します。

このbashコマンドがあります。

find . -type d -mindepth 1 -maxdepth 2 | du -h --threshold=2KB

私は次のような結果を得ました。

r2g: the tarball folders: 8.0K  ./cli
r2g: the tarball folders: 24K   ./dist/commands/ls
r2g: the tarball folders: 48K   ./dist/commands/add
r2g: the tarball folders: 36K   ./dist/commands/run
r2g: the tarball folders: 20K   ./dist/commands/basic
r2g: the tarball folders: 36K   ./dist/commands/config/local
r2g: the tarball folders: 36K   ./dist/commands/config/global
r2g: the tarball folders: 92K   ./dist/commands/config
r2g: the tarball folders: 4.0K  ./dist/commands/find
r2g: the tarball folders: 44K   ./dist/commands/init
r2g: the tarball folders: 272K  ./dist/commands
r2g: the tarball folders: 416K  ./dist
r2g: the tarball folders: 48K   ./assets
r2g: the tarball folders: 504K  .

ただし、このコマンドを単独で実行する場合:

find . -type d -mindepth 1 -maxdepth 2

私はこれだけを得ます:

r2g: the tarball folders: ./assets
r2g: the tarball folders: ./cli
r2g: the tarball folders: ./dist
r2g: the tarball folders: ./dist/commands

何が起こっているのか知っている人はいますか? du コマンドが find が明示的に渡さなくても、深さ 2 のフォルダのサブフォルダを表示するようです。どういうわけかduを深さに制限する必要がありますか?

ベストアンサー1

duユーティリティにディレクトリパス名を指定すると、デフォルトでそのディレクトリとすべてのサブディレクトリのサイズが表示されます。

ディレクトリが指定されていない場合(例コードのように)、現在のディレクトリとすべてのサブディレクトリのサイズが表示されます。

ユーティリティは標準入力から読み取られないduため、サンプルコードは何も渡しません。du

各ディレクトリのサイズを計算して使用しようとします。

find . -mindepth 1 -maxdepth 2 -type d -exec du -h --threshold=2KB -s {} \;

duそれ自体が繰り返されるので、計算には各サブディレクトリのサイズも含まれますが、-s全体のサイズのみが表示されます。

各ディレクトリのサイズを計算しますか?入らないようにするサブディレクトリのサイズを確認するには、duディレクトリ内のファイルのファイル名を明示的に渡す必要があります。これを正確にするのは少し難しいので、必ず必要な場合findでなければしませんfind

おすすめ記事