 |
 |
Counting files in a directory?
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status:
Offline
|
|
Hi there, I'm writing a program and really need to know how many files are in a directory (which is specified by the user). The way I'm doing it now works quite well (perfectly accurate about 98% of the time) but takes a long time depending on the size of the directory. Does anyone know of a better way than this:
Code:
ls -laR dirToCheck | grep -v ^d | awk '{print $9}' | grep -v ^\"..\"$ | grep -v ^\".\"$ | grep . | wc -l
Recursively list the directory, don't print directory names, only print column 9 (file names), don't print "this" and "parent" (. and ..), print everything else, and count the number of lines.
Even on a modest Applications folder, it takes forever to complete.
Many many thanks if you can help.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
What language are you writing the program in? There must be better ways to do this.
If you are stuck with shell commands, could you write the output of ls into a file then use wc (word count) to get the number of lines in the file?
[EDIT]
Duh, just looked at your command line again and you are using wc. Oh well.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Don't know if it's any faster, but this is simpler:
Code:
find dirToCheck -type f | wc -l
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status:
Offline
|
|
Originally posted by Arkham_c:
Don't know if it's any faster, but this is simpler:
Code:
find dirToCheck -type f | wc -l
Thank you, that's much simpler, about 30 seconds faster (to count 120 thousand files), and more accurate than my method.
I'll certainly be using that instead, but I still don't believe it should take over a minute and a half to complete this task on a 733MHz G4 with 896MB RAM and nothing else running at the time.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Mar 2004
Status:
Offline
|
|
could you possibly use the database used by locate? It's certainly faster... although if you needed up-to-the-minute (or hour, or day... or possibly week) accuracy, you'd have to update the db first, probably cancelling most (or all, or even more than all) of the speed boost.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|