可以使用樹來列出每個層級的檔案數嗎?

可以使用樹來列出每個層級的檔案數嗎?

我需要查看特定目錄並列出每個層級的檔案數。目錄相當大,大約有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.

答案2

我懷疑是否tree能做到這一點。然而,find可以:

find . -mindepth 3 -maxdepth 3 -type f | wc -l

將傳回等級 3 的檔案數。

答案3

tree | sed 's/ //g;s/`/\|/g;s/-.*//g' | sort | uniq -c | grep \|

結果:

     35 |
    186 ||
   1408 |||
    691 ||||

豎線 (|) 字元表示深度。

相關內容