2014-06-19 39 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

相關問題