linux C 读取目录文件并统计文件数
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#define MAX 1024
int get_file_count(char *root)
{
DIR *dir;
struct dirent * ptr;
int total = 0;
char path[MAX];
dir = opendir(root); /* 打开目录*/
if(dir == NULL)
{
perror("fail to open dir");
exit(1);
}
errno = 0;
while((ptr = readdir(dir)) != NULL)
{
//顺序读取每一个目录项;
//跳过“..”和“.”两个目录
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
{
continue;
}
//printf("%s%s\n",root,ptr->d_name);
//如果是目录,则递归调用 get_file_count函数
if(ptr->d_type == DT_DIR)
{
sprintf(path,"%s%s/",root,ptr->d_name);
//printf("%s\n",path);
total += get_file_count(path);
}
if(ptr->d_type == DT_REG)
{
total++;
printf("%s%s\n",root,ptr->d_name);
}
}
if(errno != 0)
{
printf("fail to read dir"); //失败则输出提示信息
exit(1);
}
closedir(dir);
return total;
}
int main(int argc, char * argv[])
{
int total;
if(argc != 2)
{
printf("wrong usage\n");
exit(1);
}
total = get_file_count(argv[1]);
printf("%s ha %d files\n",argv[1],total);
return 0;
}
相关文档:
压缩命令:
*.Z compress 程式壓縮的檔案;
*.bz2 bzip2 程式壓縮的檔案;
*.gz gzip 程式壓縮的檔案;
*.tar tar 程式打包的資料, ......
指令名称 : chown
使用权限 : root
使用方式 :
chown [-cfhvR] [--version] user[:group] file...
说明 : 利用 chown 可以将档案的拥有者加以改变。这个指令只有是由系统管理者(root)所使用,一般使用者没有权限可以改变别人的档案拥有者,也没有权限可以自己的档案拥有者改设为别人。只有系统管理者(root)才有这样 ......
正式实习一周,有点感触。本来就没指望能成为什么技术大牛,用自己调侃的话来说就是“混口饭吃”。不过老大对me的期望值那么高,经理管得这么严,压力颇大。对自己挺失望,没能开好头,有点对不起这么成功的老大。还好没准备做什么大事,要不然这性格得费多大力气才能重塑。。。
都是逼出来的,被经理训后,一天 ......
我就是一个小新手+小菜鸟,正在发奋学习linux,初有心得遂决定记录下来,也许对有些人来说我写的都太浅显,但是对初学者来说绝对是有用的,可以让你少走很多弯路,况且我会由浅入深的,沿着我的足迹一起学习吧,加油!
本来想先写系统安装,然后环境配置, ......