2

Recursively find the number of files in a directory.

For example, if directory one has directory 12 directories and each of these 12 directories have another 13 directories. and the grandchild (13 directories) has a fixed number of files. How do I find how many it has?

I am trying to visualize the output in the following fashion:

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

3 Answers 3

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

and for your specific formatting:

find . -type d -exec sh -c 'n=$(find "$0" -maxdepth 1 -type f -printf x | wc -c); printf "%s -- %s files\n" "$0" "$n"' {} \; 
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

find . -type f | wc -l

(it counts the output lines (wc-l) of a the output of a command (find . -type f) that outputs a line per file in the tree)

[EDIT] After reading your comment and updates I came up with this one liner:

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

3 Comments

That just gave the output of the number of files. I want per folder.
A few problems in this answer: the output of tree is not meant to be parsed; will miserably fail with spaces (lacks of quotes) or other funny symbols (newlines).
In that case I guess it could be replaced with find . -type d then. Anyway the accepted answer is much better than mine :-)
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 

1 Comment

I'm the author. I thought this was one way of doing it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.