트리를 사용하여 레벨당 파일 수를 나열할 수 있습니까?

트리를 사용하여 레벨당 파일 수를 나열할 수 있습니까?

특정 디렉토리를 살펴보고 레벨당 파일 수를 나열해야 합니다. 디렉토리는 꽤 크며 약 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 ||||

파이프(|) 문자는 깊이를 나타냅니다.

관련 정보