Recursively count all the files in a directory [duplicate]
find . -type f | wc -l
Explanation:find . -type f
finds all files ( -type f ) in this ( . ) directory and in all sub directories, the filenames are then printed to standard out one per line.
This is then piped | into wc
(word count) the -l
option tells wc to only count lines of its input.
Together they count all your files.
The answers above already answer the question, but I'll add that if you use find without arguments (except for the folder where you want the search to happen) as in:
find . | wc -l
the search goes much faster, almost instantaneous, or at least it does for me. This is because the type clause has to run a stat() system call on each name to check its type - omitting it avoids doing so.
This has the difference of returning the count of files plus folders instead of only files, but at least for me it's enough since I mostly use this to find which folders have huge ammounts of files that take forever to copy and compress them. Counting folders still allows me to find the folders with most files, I need more speed than precision.
For files:
find -type f | wc -l
For directories:
find -mindepth 1 -type d | wc -l
They both work in the current working directory.