ツリーを使用して各レベルのファイル数を一覧表示できますか?

ツリーを使用して各レベルのファイル数を一覧表示できますか?

特定のディレクトリを調べて、各レベルのファイル数を一覧表示する必要があります。ディレクトリの深さは約10〜15レベルほどかなり大きくなります。たとえば、次のような場合:

D1
|
|-- D2A (5 files in this directory)
|    |-- D3A (6 files in this directory)
|    |-- D3B (7 Files in this directory)
|
|-- D2B (1 file in this directory)

これにより、レベル3には13個のファイルがあり、レベル2には6個のファイルがあることがわかります(または6+13は重要ではありません)。これは可能ですかTree?オプションを混ぜて試してみましたが、うまくいかないようです。

ベストアンサー1

find . -type d | \
perl -ne 'BEGIN{ sub cnt{ $file=shift; $c="find $file -maxdepth 1 -type f | wc -l";int(`$c`) }} chomp; printf "%s %s\n", $_, cnt($_)' | \
perl -ne '/^(.*) (\d*)$/; $_{scalar(split /\//, $1)}+=$2; END { printf "Depth %d has %d files.\n", @$_ for map { [$_,$_{$_}] } sort keys %_ }'

結果:

Depth 1 has 7 files.
Depth 2 has 2353 files.
Depth 3 has 2558 files.
Depth 4 has 8242 files.
Depth 5 has 6452 files.
Depth 6 has 674 files.
Depth 7 has 1112 files.
Depth 8 has 64 files.
Depth 9 has 154 files.

おすすめ記事