linux遍历目录及其子目录
2016-03-07 14:11
316 查看
1 在linux下遍历某一目录下内容LINUX下历遍目录的方法一般是这样的
2 打开目录->读取->关闭目录
3 相关函数是opendir -> readdir -> closedir,其原型如下:
4 #include
5 DIR *opendir(const char *dirname);
6 struct dirent *readdir(DIR *dirp);
7 int closedir(DIR *dirp);
1 dirent结构体定义:
struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
其中inode表示存放的是该文件的结点数目(具体可了解linux下的文件系统),d_off 是文件在目录中的编移,这两个基本很少用。
d_type表示档案类型:
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};
d_reclen认为是纪录的长度,计算方式应该是4(d_ino)+4(d_off)+2(d_reclen)+1(d_type)+1(补齐位)+4N(d_name会自动补齐:
1.jpg为8,12.jpg也为8,1234.jpg也为8,12345.jpg则为12);所以一般d_reclen是20和24(其中.和..是16)。
d_name表示文件名,如test.jpg
unsigned char d_type,文件或目录的类型。它有可能的取值如下:
DT_UNKNOWN,未知的类型
DT_REG,普通文件
DT_DIR,普通目录
DT_FIFO,命名管道或FIFO
DT_SOCK,本地套接口
DT_CHR,字符设备文件
DT_BLK,块设备文件
linux C 遍历目录及其子目录 举例:
2 打开目录->读取->关闭目录
3 相关函数是opendir -> readdir -> closedir,其原型如下:
4 #include
5 DIR *opendir(const char *dirname);
6 struct dirent *readdir(DIR *dirp);
7 int closedir(DIR *dirp);
1 dirent结构体定义:
struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
其中inode表示存放的是该文件的结点数目(具体可了解linux下的文件系统),d_off 是文件在目录中的编移,这两个基本很少用。
d_type表示档案类型:
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};
d_reclen认为是纪录的长度,计算方式应该是4(d_ino)+4(d_off)+2(d_reclen)+1(d_type)+1(补齐位)+4N(d_name会自动补齐:
1.jpg为8,12.jpg也为8,1234.jpg也为8,12345.jpg则为12);所以一般d_reclen是20和24(其中.和..是16)。
d_name表示文件名,如test.jpg
unsigned char d_type,文件或目录的类型。它有可能的取值如下:
DT_UNKNOWN,未知的类型
DT_REG,普通文件
DT_DIR,普通目录
DT_FIFO,命名管道或FIFO
DT_SOCK,本地套接口
DT_CHR,字符设备文件
DT_BLK,块设备文件
linux C 遍历目录及其子目录 举例:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> #include <iostream> using namespace std; void listDir(char *path) { DIR *pDir; struct dirent *ent; int i=0; char childpath[512]; pDir=opendir(path); memset(childpath,0,sizeof(childpath)); while((ent=readdir(pDir))!=NULL){ if(ent->d_type & DT_DIR){ if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0) continue; sprintf(childpath,"%s/%s",path,ent->d_name); printf("path:%s\n",childpath); listDir(childpath); } else{ cout<<ent->d_name<<endl; } } } int main(int argc,char *argv[]) { listDir(argv[1]); return 0; }
相关文章推荐
- centos7防火墙
- 浅析 Linux 初始化 init 系统,第 1 部分: sysvinit 第 2 部分: UpStart 第 3 部分: Systemd
- 在centos 下安装配置基于gitosis 的git 服务
- [Linux]从控制台一次读取一个字符,无需等待回车键
- Linux文件种类和扩展名
- linux下cscope的使用
- linux下cscope 使用方法
- linux ps top 命令 VSZ,RSS,TTY,STAT, VIRT,RES,SHR,DATA的含义【转】
- Deepin linux下安装netbeans
- CentOS6.5基于snort+barnyard2+base的 入侵检测系统的搭建
- Linux初学:(一)Linux概述
- linux查看磁盘空间
- How To Install Zabbix Server On CentOS 7
- Linux解压/压缩命令总结
- 嵌入式 armlinux内核编译注意事项
- Linux内核编译与裁剪(ARM版)
- CentOS 7安装MySQL
- Linux下创建与解压tar, tar.gz和tar.bz2文件及压缩率对比 | 沉思小屋
- Linux 病毒跑满带宽处理
- 启动Mediation_Function失败及解决方案