递归查找目录中的文件数

时间:2014-06-19 15:28:57

标签: linux bash shell command-line

递归查找目录中的文件数。

例如,如果目录one具有目录12 directories,并且每个12 directories都有另一个13 directories。 孙子(13个目录)有固定数量的文件。 我如何找到它有多少?

我试图以下列方式可视化输出:

a/b/1/ -- 50 files
a/b/2/ -- 10 files
a/c/1/ -- 20 files.

3 个答案:

答案 0 :(得分:3)

find . -type d -exec sh -c 'printf "%s " "$0"; find "$0" -maxdepth 1 -type f -printf x | wc -c' {} \;

并针对您的具体格式:

find . -type d -exec sh -c 'n=$(find "$0" -maxdepth 1 -type f -printf x | wc -c); printf "%s -- %s files\n" "$0" "$n"' {} \;

答案 1 :(得分:1)

尝试:

find . -type f | wc -l

(它计算命令输出的输出行(wc-l)(find。-type f),在树中为每个文件输出一行)

[编辑] 在阅读完您的评论和更新后,我想出了这个内容:

tree -idf --noreport | while read line ; do echo $line ; find $line -maxdepth 1 -type f | wc -l ; done

答案 2 :(得分:0)

for a in 1000000 1200000 1400000 1600000 1800000 2000000 2200000 2400000 800000
    do
    for b in 10 14 18 2 22 26 30 34 38 42 46 6
    do
        echo $a-$b
        find $a/$a-$b/ -type f -print | wc -l
    done
done
相关问题