Linux_C目录扫描程序
2014-03-03 17:50
267 查看
说白了就是对整棵目录树进行深度优先遍历即可。
#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <stdlib.h> void printdir(const char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr, "cannot open directory: %s\n", dir); return ; } chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(entry->d_name, ".") == 0 || \ strcmp(entry->d_name, "..") == 0 ) { continue; } /*当前目录和上一级目录跳过*/ printf("%*s%s/\n", depth, "", entry->d_name); printdir(entry->d_name, depth + 4); } } chdir(".."); closedir(dp); return ; } int main() { const char *dir = "/home"; printf("Directory scan of /home:\n"); printdir(dir, 0); printf("done.\n"); return 0; }
相关文章推荐
- 个性化修改Linux登录时的字符界面
- Linux编辑器vi使用方法详细介绍
- 深入剖析Linux系统启动流程和系统初始化
- linux下使用gcc编译Nate Robin的OpenGL教程
- ARM Linux 3.x的设备树(Device Tree)
- CentOS6里面安装Ngnix
- linux学习(四)--用户与组命令
- linux学习(三)--基本命令
- Linux与Windows下的Java开发区别
- Linux内核模块传递参数
- LinuxCast学习笔记四Filesys_Arch
- Linux下线程池的创建
- Linux下,Java进程占用CPU超100%
- 【linux学习笔记】打印出最常使用的10条命令
- Linux 杀死一个进程
- Linux命令之cat
- Linux下,查看并设置系统时间
- Linux Documentation学习笔记 -- 写在最前面
- Linux常用命令大全
- CentOS永久修改主机名