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?
The simplest way:
find <dir> -type f | wc -l
Slightly faster, perhaps:
find <dir> -type f -printf '\n' | wc -l
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With