I have a really deep directory tree on my Linux box. I would like to count all of the files in that path, including all of the subdirectories.

For instance, given this directory tree:

/home/blue
/home/red
/home/dir/green
/home/dir/yellow
/home/otherDir/

If I pass in /home, I would like for it to return 4 files. Or, bonus points if it returns 4 files, 2 directories. Basically, I want the equivalent of right-clicking a folder on Windows and selecting properties and seeing how many files/folders are contained in that folder.

How can I most easily do this? I have a solution involving a Python script I wrote, but why isn't this as easy as running ls | wc or similar?

link|improve this question

60% accept rate
Not exactly what you're looking for, but to get a very quick grand total, if your locate database is up to date: locate /some/path | wc -l (or on my Mac: locate -c /some/path). But: this will also count files in /this/other/path/with/some/path, and will count the folders themselves. – Arjan Nov 13 '10 at 9:44
feedback

5 Answers

up vote 12 down vote accepted

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.

link|improve this answer
Delicious. Thanks! 8 minutes till I can accept this. :) – omghai2u Oct 12 '10 at 21:47
feedback

For files:

find -type f | wc -l

For directories:

find -mindepth 1 -type d | wc -l

They both work in the current working directory.

link|improve this answer
If I could up-vote, I'd upvote, you too. – omghai2u Oct 12 '10 at 22:13
feedback

Slight update to accepted answer, if you want a count of dirs and such

find $DIR -exec stat -c '%F' {} \; | sort | uniq -c | sort -rn
link|improve this answer
On a Mac/BSD: find $DIR -exec stat -f '%HT' {} \; | sort | uniq -c | sort -rn (Here, type %T is empty for a regular file, an asterisk for an executable file, or a slash for a folder; hence %HT is needed, or some extra text to avoid the space from not being counted, like > %T.) – Arjan Nov 13 '10 at 10:03
feedback

With bash 4+

shopt -s globstar
for file in **/*
do
  if [ -d "$file" ];then
    ((d++))
  elif [ -f "$file" ];then
     ((f++))
  fi
done
echo "number of files: $f"
echo "number of dirs: $d"

No need to call find twice if you want to search for files and directories

link|improve this answer
feedback

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. Don't know why, though, it might have something to do with the files being indexed already...

This has the disadvantage 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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.