Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Counting files in a directory?

Counting files in a directory?
Thread Tools
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status: Offline
Reply With Quote
Oct 14, 2004, 06:24 AM
 
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.
ClamXav - the free virus scanner for Mac OS X | Geobunny learns to fly
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Oct 14, 2004, 12:31 PM
 
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.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Oct 14, 2004, 01:00 PM
 
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
Reply With Quote
Oct 14, 2004, 01:34 PM
 
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.
ClamXav - the free virus scanner for Mac OS X | Geobunny learns to fly
     
Mac Enthusiast
Join Date: Mar 2004
Status: Offline
Reply With Quote
Oct 14, 2004, 09:52 PM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:51 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2