2014-06-19 37 views
2

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

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

我试图想象下面的方式输出:

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

回答

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"' {} \; 
+0

'_/\ _'放下手!比我的工作好得多! – pistal

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

+0

这只是给了文件的数量的输出。我想要每个文件夹。 – pistal

+0

更新了我的答案。 – NotGaeL

+0

在这个答案中的几个问题:“树”的输出不意味着被解析;将无法用空格(缺乏引号)或其他有趣的符号(换行符)失败。 –

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 
+0

我是作者。我认为这是一种做法。 – pistal

相关问题