I used this before for a dir with ~4million files, this takes around 0.5s while ls takes... well... no idea, it never finishes.
compile with "g++ count.cpp -o count && mv count /usr/sbin"
Usage: ./count "<path>"
compile with "g++ count.cpp -o count && mv count /usr/sbin"
Usage: ./count "<path>"
Code:
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Usage: ./count \"<path>\"\n");
return 1;
}
struct dirent *de;
DIR *dir = opendir(argv[1]);
if(!dir)
{
printf("opendir() failed! Does it exist?\n");
return 1;
}
unsigned long count=0;
while(de = readdir(dir))
{
++count;
}
closedir(dir);
printf("%lu\n", count);
return 0;
}