don't kow how to do a shell script but use this
c is lots faster
name it count_files.c
or whatever you want
compile with
gcc -o count_files count_files.c
then put the executable in usr/bin or wherever
you can also call this from a script of course
count_files will get you the regular files in current working directory
or you can put a path to a directory as an argument after the command
if you want more than regular files counted change it or write back
Code:
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int get_file_type (const char* path)
{
struct stat st;
lstat (path, &st);
if (S_ISREG (st.st_mode))
return 0;
else return 1;
}
int main (int argc, char* argv[])
{
char* dir_path;
DIR* dir;
struct dirent* entry;
char entry_path[PATH_MAX + 1];
size_t path_len;
int type;
int count = 0;
if (argc >= 2)
dir_path = argv[1];
else
dir_path = ".";
strncpy (entry_path, dir_path, sizeof (entry_path));
path_len = strlen (dir_path);
if (entry_path[path_len - 1] != '/') {
entry_path[path_len] = '/';
entry_path[path_len + 1] = '\0';
++path_len;
}
dir = opendir (dir_path);
while ((entry = readdir (dir)) != NULL) {
strncpy (entry_path + path_len, entry->d_name,
sizeof (entry_path) - path_len);
type = get_file_type (entry_path);
if (type == 0) count++;
}
printf ("%i\n", count);
closedir (dir);
return 0;
}