在Linux中如何统计目录内文件
发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,这篇文章主要介绍了在Linux中如何统计目录内文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。//调用opendir和readdir
千家信息网最后更新 2024年10月18日在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安全错误
数据库的锁怎样保障安全
查数据库的数据需要什么
使用ADO数据库技术
司法部仲裁案例数据库
数据库表隐藏
西城区大型软件开发经历
软件开发入门工作
云服务器图片显示不出来
广东服务器分销商
虹口区营销软件开发卖价
网络安全与防范意识内容
中国网络技术大学官网
大型网站服务器配置
自媒体网络技术咨询资讯
网络安全体系结构大概描述
服务器安装机柜
go桌面软件开发
游戏平台服务器
软件开发的形式化方法优缺点
数据库表上的数据怎么输进去
应用级软件开发会被淘汰吗
我的世界服务器手机版推荐有难度
网络安全作文议论文800字
西南软件开发专业大学排名
优质的香港服务器
3d建模数据库
打服务器管理器
网络安全vim的作用
服务器f5
烟台开发区网络技术有限公司
数据库展示找不到表是什么原因