linux目录操作及递归遍历目录

目录相关函数介绍

//mkdir 函数创建目录

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

//rmdir 删除目录

#include <unistd.h>
int rmdir(const char *pathname);

//dopendir/fdopendir  //打开目录

DIR是一个结构体,是一个内部结构,用来存储读取目录的相关信息。

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

//readdir 读目录

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

struct dirent {
    ino_t d_ino; /* inode number */
    off_t d_off; /* offset to the next dirent */
    unsigned short d_reclen; /* length of this record */
    unsigned char d_type; /* type of file; not supportedby all file system types */
    char d_name[256]; /* filename */
};

readdir 每次返回一条记录项,,DIR*指针指向下一条记录项。

//rewinddir

#include <sys/types.h>
#include <dirent.h>

void rewinddir(DIR *dirp);

把目录指针恢复到目录的起始位置。

//telldir函数

 #include <dirent.h>

 long telldir(DIR *dirp);

函数返回值是为目录流的当前位置,表示目录文件距开头的偏移量。

//seekdir

#include <dirent.h>
void seekdir(DIR *dirp, long offset);

seekdir表示设置文件流指针位置。

//closedir 关闭目录流

 #include <sys/types.h>
 #include <dirent.h>

 int closedir(DIR *dirp);

使用递归来遍历目录下的文件

#include<stdio.h>
#include <errno.h>
#include<stdlib.h>
#include<string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_PATH 512

void print_file_info(char *pathname);
void dir_order(char *pathname);

void dir_order(char *pathname)
{
	DIR *dfd;
	char name[MAX_PATH];
	struct dirent *dp;
	if ((dfd = opendir(pathname)) == NULL)
	{
		printf("dir_order: can't open %s\n %s", pathname,strerror(errno));
		return;
	}
	while ((dp = readdir(dfd)) != NULL)
	{
		if (strncmp(dp->d_name, ".", 1) == 0)
			continue; /* 跳过当前目录和上一层目录以及隐藏文件*/
		if (strlen(pathname) + strlen(dp->d_name) + 2 > sizeof(name))
		{
			printf("dir_order: name %s %s too long\n", pathname, dp->d_name);
		} else
		{
			memset(name, 0, sizeof(name));
			sprintf(name, "%s/%s", pathname, dp->d_name);
			print_file_info(name);
		}
	}
	closedir(dfd);

}
void print_file_info(char *pathname)
{
	struct stat filestat;
	if (stat(pathname, &filestat) == -1)
	{
		printf("cannot access the file %s", pathname);
		return;
	}
	if ((filestat.st_mode & S_IFMT) == S_IFDIR)
	{
		dir_order(pathname);
	}
	printf("%s %8ld\n", pathname, filestat.st_size);
}
int main(int argc, char *argv[])
{
	if (argc == 1)
	{
		dir_order(".");
	} else
	{
		dir_order(argv[1]);
	}
	return 0;
}

版权声明:欢迎转载,如有不足之处,恳请斧正。

时间: 2024-08-07 00:08:40

linux目录操作及递归遍历目录的相关文章

Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。

题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就使用递归遍历目录 代码如下: 1 import java.io.File; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 //创建file对象 6 File f=new File("d://新建文件夹&qu

文件操作-正确的遍历目录的方法

1. 看手册的例子说的 very good 在删除目录 和 遍历目录的时候要注意了,应该很少人把名字取为0吧. //  正确的遍历目录的方法 while(false !==( $file= readdir($file_path) ) ){ } //错误的遍历目录的方法 while($file=readdir($file_path)){ } 偶之前就一直使用这个错误的方法 ------------------ 全等 ===  ,不全等 :  !==    值不等,类型不等,值&类型都不等.. re

Python递归遍历目录下所有文件

#自定义函数: import ospath="D:\\Temp_del\\a" def gci (path): parents = os.listdir(path) for parent in parents: child = os.path.join(path,parent) #print(child) if os.path.isdir(child): gci(child) # print(child) else: print(child) gci(path) #使用os.walk方

(实用篇)PHP不用递归遍历目录下所有文件的代码

<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ function scanfiles($dir) { if (! is_dir ( $dir )) return array (); // 兼容各操作系统 $dir = rtrim ( str_replace ( '\\', '/', $dir ), '/' ) . '/'; // 栈,默认值为传入的目录 $

递归遍历目录拷贝cdh下的lib到一个目录

destpath='/home/hadoop/soft/hadoop-2.0.0-cdh4.5.0/cdhlib/'jarpath='/home/hadoop/soft/hadoop-2.0.0-cdh4.5.0/share/hadoop/'search='jar' iterdir(){    cd $1    for p in `ls`; do        echo "$p" |grep -q "$search"        if [ $? -eq 0 ]  

Java 之递归遍历目录

Java 之递归遍历目录 一.内容 输出指定目录(文件夹)下的所有文件(包括目录)的绝对路径 二.源代码:RecursiveListDirectory.java 1 package cn.com.zfc.day016; 2 3 import java.io.File; 4 5 /** 6 * @describe 递归遍历目录 7 * @author zfc 8 * @date 2018年1月1日 上午8:44:55 9 */ 10 public class RecursiveListDirect

java递归遍历目录获取所有文件及目录方案

本文提供一份递归遍历目录获取所有文件及目录的源代码: import java.io.File; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2019/2/10. */ public class TestWalkDir { static class FileComponent { File curFile; List<FileComponent> fileComponen

Makefile 递归遍历目录(含子目录) 编译动态库

这里推荐一本书,Makefile手册,本人正在学习,多交流~ 一.统一编译所有子目录的文件 直接上Makefile内容了, AR=arLD=ldCC=gcc CFLAGS = -O2 -Wall  -I./Test \                -I./Test/Test1 \ #注:"\"后面不能有空格,并且该句写完后最好有个换行 #注释部分推荐在单独的一行编写 #动态库需要 -fPIC  -shared SOFLAGS = -O2 -fPIC -sharedTARGET = .

php 递归和非递归遍历目录下的所有文件

//php 递归实现遍历 用dir 返回对象 <?    function loop($dir){  $mydir =dir($dir);    //以对象的形式访问     while($file = $mydir ->read()){                         //目录中有隐藏文件'.'和'..' 遍历的时候需要注意             if((is_dir("$dir/$file")) && ($file!=".&q