pistal
pistal

Reputation: 2456

Recursively find the number of files in a directory

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.

Upvotes: 2

Views: 161

Answers (3)

NotGaeL
NotGaeL

Reputation: 8484

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

Upvotes: 1

pistal
pistal

Reputation: 2456

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

Upvotes: 0

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

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

Upvotes: 3

Related Questions