How to count files in directory in Linux
This article mainly introduces how to count the files in the catalog in Linux, which has a certain reference value, and interested friends can refer to it. I hope you will get a lot after reading this article.
/ call opendir and readdir functions to traverse the specified directory / / then print out the number of files of various types in the specified directory # include # include typedef int Myfunc (const char *, const struct stat *, int); / / define a function static Myfunc myfunc;static int myftw (char *, Myfunc *); static int dopath (Myfunc *); static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot / / variable char * path_alloc (int* size) corresponding to the number of files of various types; int main (int argc, char * argv []) {int ret; if (argc! = 2) {printf ("Please enter correct parameters!\ n"); / / Parameter error return 1;} ret = myftw (argv [1], myfunc) / * does it all * / ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock; / / calculate the total number of files if (ntot = = 0) / / set ntot to 1 if there are no files in the directory to avoid a divisor of 0 {ntot = 1 } / / print data for various types of files printf ("ordinary file =% 7ld,% 5.2f%\ n", nreg, nreg*100.0/ntot); printf ("directory file =% 7ld,% 5.2f%\ n", ndir,ndir*100.0/ntot); printf ("block device file =% 7ld,% 5.2f%\ n", nblk,nblk*100.0/ntot) Printf ("word device file =% 7ld,% 5.2f%\ n", nchr, nchr*100.0/ntot); printf ("FIFOs =% 7ld,% 5.2f%\ n", nfifo,nfifo*100.0/ntot); printf ("symbolic link file =% 7ld,% 5.2f%\ n", nslink, nslink*100.0/ntot) Printf ("socket file =% 7ld,% 5.2f%\ n", nsock,nsock*100.0/ntot); return ret;} / / path buffer allocation function char * path_alloc (int* size) {char * p = NULL; if (! size) {return NULL;} p = malloc; if (p) {* size = 256;} else {* size = 0;} return p } # define FTW_F 1 / / # define FTW_D 2 / / directory # define FTW_DNR 3 / / unreadable directory # define FTW_NS 4 / / static char * fullpath; / / the full path to each file static int myftw (char * pathname, Myfunc * func) {int len; fullpath = path_alloc (& len) / / assign a length strncpy (fullpath, pathname, len) to the path buffer; / / copy the file name fullpath [len-1] = 0; return (dopath (func));} / get the file status static int dopath (Myfunc* func) {struct stat statbuf; struct dirent * dirp; DIR * dp; int ret; char * ptr; if (lstat (fullpath, & statbuf))
< 0) //获得文件状态失败 { return(func(fullpath, &statbuf, FTW_NS)); } if (S_ISDIR(statbuf.st_mode) == 0) //如果不是目录 { return(func(fullpath, &statbuf, FTW_F)); } if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) { return(ret); } ptr = fullpath + strlen(fullpath); //指向路径缓冲区结尾 *ptr++ = '/'; *ptr = 0; if ((dp = opendir(fullpath)) == NULL) //如果不能读目录 { return(func(fullpath, &statbuf, FTW_DNR)); } while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->D_name, ".") = = 0 | | strcmp (dirp- > d_name, "..") = = 0) continue; / * ignore dot and dot-dot * / strcpy (ptr, dirp- > d_name) / * append name after slash * / if ((ret = dopath (func))! = 0) / * recursive * / break; / * time to leave * /} ptr [- 1] = 0; / * erase everything from slash onwards * / if (closedir (dp))
< 0) { printf("can't close directory %s\n", fullpath); } return(ret);}static int myfunc(const char *pathname, const struct stat *statptr, int type){ switch (type) { case FTW_F: switch (statptr->St_mode & S_IFMT) {case S_IFREG: nreg++; break; case S_IFBLK: nblk++; break; case S_IFCHR: nchr++; break; case S_IFIFO: nfifo++; break; case S_IFLNK: nslink++; break Case S_IFSOCK: nsock++; break; case S_IFDIR: printf ("for S_IFDIR for% s\ n", pathname);} break; case FTW_D: ndir++; break Case FTW_DNR: printf ("can't read directory% s\ n", pathname); break; case FTW_NS: printf ("stat error for% s\ n", pathname); break; default: printf ("unknown type% d for pathname% s\ n", type, pathname) } return (0);} Thank you for reading this article carefully. I hope the article "how to count the documents in the catalogue in Linux" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!