Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fastest way to count the number of files in a directory (including subdirectories)

I'm running a script that looks at all the files in a directory and its subdirectories.

The script has been running for a day, and I'd like to estimate how long it will keep running. I know how many files it processed so far (73,000,000), but I don't know the total number of files.

What is the fastest way to count the files?

I tried right-clicking on the directory and selecting "properties", and it's slowly counting up. I tried redirecting ls into a file, and it's just churning & churning...

Should I write a program in c?

like image 977
Ada Lovelace Avatar asked Jun 04 '15 19:06

Ada Lovelace


2 Answers

The simplest way:

find <dir> -type f | wc -l

Slightly faster, perhaps:

find <dir> -type f -printf '\n' | wc -l
like image 97
John Kugelman Avatar answered Sep 29 '22 13:09

John Kugelman


I did a quick research. Using a directory with 100,000 files I compared the following commands:

ls -R <dir>
ls -lR <dir>
find <dir> -type f

I ran them twice, once redirecting into a file (>file), and once piping into wc (|wc -l). Here are the run times in seconds:

        >file   |wc
ls -R     14     14
find      89     56
ls -lR    91     82

The difference between >file and |wc -l is smaller than the difference between ls and find.

It appears that ls -R is at least 4x faster than find.

like image 22
Ada Lovelace Avatar answered Sep 29 '22 11:09

Ada Lovelace