在Linux中如何统计目录内文件
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了在Linux中如何统计目录内文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。//调用opendir和readdir
千家信息网最后更新 2026年11月02日在Linux中如何统计目录内文件
这篇文章主要介绍了在Linux中如何统计目录内文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
//调用opendir和readdir函数对指定目录进行遍历操作//然后打印输出指定目录中各种类型的文件数目#include#include #include #include #include #include #include typedef int Myfunc(const char *, const struct stat *, int); //定义一个函数static Myfunc myfunc;static int myftw(char *, Myfunc *);static int dopath(Myfunc *);static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;//各种类型的文件数目对应的变量char *path_alloc(int* size);int main(int argc, char *argv[]){ int ret; if (argc != 2) { printf("请输入正确的参数!\n"); //参数错误 return 1; } ret = myftw(argv[1], myfunc); /* does it all */ ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock; //计算文件总量 if (ntot == 0) //如果目录中没有文件则将ntot设置为1以避免除数为0 { ntot = 1; } //以下一次打印各种类型文件的数据 printf("普通文件 = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot); printf("目录文件 = %7ld, %5.2f %%\n", ndir,ndir*100.0/ntot); printf("块设备文件 = %7ld, %5.2f %%\n", nblk,nblk*100.0/ntot); printf("字设备文件 = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot); printf("FIFOs = %7ld, %5.2f %%\n", nfifo,nfifo*100.0/ntot); printf("符号链接文件 = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot); printf("套接字文件 = %7ld, %5.2f %%\n", nsock,nsock*100.0/ntot); return ret;}//路径缓冲区分配函数char *path_alloc(int* size){ char *p = NULL; if(!size) { return NULL; } p = malloc(256); if(p) { *size = 256; } else { *size = 0; } return p;}#define FTW_F 1 //#define FTW_D 2 //目录#define FTW_DNR 3 //不能读的目录#define FTW_NS 4 //不能获得状态的文件static char *fullpath; //存放每个文件完整路径static int myftw(char *pathname, Myfunc *func){ int len; fullpath = path_alloc(&len); //给路径缓冲区分配一个长度 strncpy(fullpath, pathname, len); //复制文件名称 fullpath[len-1] = 0; return(dopath(func));}//获得文件的状态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);}
感谢你能够认真阅读完这篇文章,希望小编分享的"在Linux中如何统计目录内文件"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
文件
目录
篇文章
函数
类型
路径
统计
参数
数目
状态
缓冲区
设备
分配
缓冲
普通
价值
兴趣
变量
同时
名称
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
南京欣网互联网络科技
互联网新科技
百单网互联网科技有限公司怎么样
互联网科技深圳
圆梦互联网科技
亿策互联网科技
丰台科技园 互联网
深圳期待互联网科技
成都仁贤领冠互联网科技有限公司
上海电发互联网科技有限公司
重庆威客帮互联网科技
崇明区互联网教育科技售后服务
本届互联网博览会上的黑科技
民营互联网公司国防科技
每天都有哪些好的互联网科技
广州燕处互联网科技有限公司
广东鹅鹅鹅互联网科技有限公司
中国互联网公司的科技投入
互联网科技品牌
上海互联网科技园景观
手磨咖啡
上海挚品互联网科技有限公司
港股互联网科技股
伴你行互联网科技有限公司
长亮科技互联网金融
大白互联网科技
互联网科技博客
南山科技园互联网公司有哪些
阿大互联网科技有限公司
三亚科技创新互联网基地