Using bash, I can find count the count of specific extensions of a file like jpg, mp4 etc. How can I achieve this using C language system calls? Any help to achieve this using C language system calls?
ls -lR /path/to/dir/*.jpg | wc -l
Using bash, I can find count the count of specific extensions of a file like jpg, mp4 etc. How can I achieve this using C language system calls? Any help to achieve this using C language system calls?
ls -lR /path/to/dir/*.jpg | wc -l
I code for general use. You can manipulate d_name on your own.
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
/* redo input */
if ((dp = opendir(argv[1])) == NULL)
/* error opening dir */
printf("%8s %8s %8s %8s %8s\n",
"d_name", "d_fileno",
"d_reclen", "d_type",
"d_namlen");
while ((dirp = readdir(dp)) != NULL)
printf("%8s %8d %8d %8d %8d\n",
dirp->d_name, dirp->d_fileno,
dirp->d_reclen, dirp->d_type,
dirp->d_namlen);
closedir(dp);
return 0;
}
Here is an example that shows how to using the readdir()
function. You should be able to easily modify this to do what you want.