PDA

View Full Version : Fast dir files count?



cl333r
May 17th, 2009, 03:48 PM
Folks,
I thought maybe there's some (low level) way to fetch the files count in a directory? Maybe the file systems keep track of this number in some settings field?
Anyone?

Martin Witte
May 17th, 2009, 03:57 PM
As far as I know the only method to get this is to count the files, eg. with ls -a|wc -l, or find . -type f | wc -l if you want to include files in subdirectories

cl333r
May 17th, 2009, 04:10 PM
I'm using C++ and I'm iterating through each dir entry to count them all. I thought there could be a "feature" in the filesystem(s) in the form of some field telling how many entries are in a given dir thus not having to iterate through each entry, would be much faster especially for dirs with many files. In my case the speed gains are worth the "workaround".

Arndt
May 19th, 2009, 03:05 PM
I'm using C++ and I'm iterating through each dir entry to count them all. I thought there could be a "feature" in the filesystem(s) in the form of some field telling how many entries are in a given dir thus not having to iterate through each entry, would be much faster especially for dirs with many files. In my case the speed gains are worth the "workaround".

You may want to look at the file /usr/include/dirent.h, but there's no such information there.

Why are you interested in just the number of entries?

cl333r
May 19th, 2009, 06:27 PM
I'm creating a file browser, like Thunar but in C++ and faster, + a bit different, so when browsing a directory - all subdirectories should report the number of files within them, but I thought iterating through each subdir is a bit overkill and wished there was a workaround for that, would be (much) faster and avoid trashing the HDD. Since this happens almost every time one browses a given dir - I'm sure it's worth the effort, especially if the file browser is not gonna be used only by me.