Recursively count all the files in a directory

217

72

Possible Duplicate:
How can I count the number of folders in a drive using Linux?

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 four files. Or, bonus points if it returns four files and two 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?

omghai2u

Posted 2010-10-12T21:41:12.947

Reputation: 2 670

Question was closed 2012-08-20T13:41:51.907

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 – 2010-11-13T09:44:06.227

By the way, this is a different, but closely related problem (counting all the directories on a drive) and solution: http://superuser.com/questions/129088/how-can-i-count-the-number-of-folders-in-a-drive-using-linux

– Amanda – 2012-06-20T14:05:08.997

Answers

364

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.

Nifle

Posted 2010-10-12T21:41:12.947

Reputation: 31 337

1This doesn't deal with the off-by-one error because of the last newline from the find output – RobertDeRose – 2019-02-20T22:22:16.897

for counting directories ONLY, use '-type d' instead of '-type f' :D – MevlütÖzdemir – 2019-03-12T13:49:04.670

1When there are no files found, the result is 1 – Dylanthepiguy – 2019-05-30T03:15:27.690

49

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.

Francisco Vieira

Posted 2010-10-12T21:41:12.947

Reputation: 599

1When there are no files found, the result is 1 – Dylanthepiguy – 2019-05-30T03:15:56.263

26

For files:

find -type f | wc -l

For directories:

find -mindepth 1 -type d | wc -l

They both work in the current working directory.

cYrus

Posted 2010-10-12T21:41:12.947

Reputation: 18 102

1When there are no files found, the result is 1 – Dylanthepiguy – 2019-05-30T03:15:35.153

8

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

user31894

Posted 2010-10-12T21:41:12.947

Reputation: 2 245

8

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

Rich Homolka

Posted 2010-10-12T21:41:12.947

Reputation: 27 121

Gives me "find: illegal option -- e" on my 10.13.6 mac – K.-Michael Aye – 2018-10-23T01:22:15.050

1

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 – 2010-11-13T10:03:21.417