![可以使用树来列出每个级别的文件数吗?](https://linux22.com/image/51063/%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E6%A0%91%E6%9D%A5%E5%88%97%E5%87%BA%E6%AF%8F%E4%B8%AA%E7%BA%A7%E5%88%AB%E7%9A%84%E6%96%87%E4%BB%B6%E6%95%B0%E5%90%97%EF%BC%9F.png)
我需要查看特定目录并列出每个级别的文件数。该目录相当大,大约有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 ||||
竖线 (|) 字符表示深度。