遍历目录结构查找文件是很常用的功能,今天介绍一下使用Linux C 遍历Linux目录结构的方法:

  linux提供几个系统调用,以便于直接目录的读取和操作:

  DIR * opendir(const char * pathname);

  struct dirent * readdir(DIR * dir_handle);

  int closedir(DIR * dir);

  int stat(const char *file_name, struct stat *buf);

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <dirent.h>          //包含目录操作的相关函数

/**
* @Param pathname 要遍历的目录全路径名称
* @Param depth 当前的遍历等级,初始为0
*/
void printdir(const char * pathname, const int depth)
{
DIR * dir;
struct dirent * de;
struct stat fs;
int i = 0; if((dir = opendir(pathname)) == NULL)
{
printf("open dir %s error \r\n", pathname);
return;
} chdir(pathname);
while((de = readdir(dir)) != NULL)
{
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0){continue;} if(stat(de->d_name, &fs) == -1){perror("fstat error");continue;} if(S_ISDIR(fs.st_mode))
{
              /**
               * 如果当前路径是目录,就递归调用printdir函数
              */
for(i=0;i<depth;++i){printf(" ");}
printf("%s\r\n", de->d_name);
printdir(de->d_name, depth + 4);
}
else
{
for(i=0;i<depth;++i)
{
printf(" ");
} printf("%s\r\n", de->d_name);
}
}
chdir("..");
closedir(dir); return;
} int main(int argc, char ** argv)
{
printdir("/root/projects", 0); return 0;
}

Linux 自带的目录遍历函数

int scandir(const char *dir,struct dirent **namelist,int (*filter)(const void *b),

int ( * compare )( const struct dirent **, const struct dirent ** ) );
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b);

具体函数使用方法见man

Linux C语言遍历目录结构的更多相关文章

  1. 比起Windows,怎样解读Linux的文件系统与目录结构?

    比起Windows,怎样解读Linux的文件系统与目录结构? Linux 和Windows的文件系统有些不同,在学习使用 Linux 之前,若能够了解这些不同,会有助于后续学习. 本文先对Window ...

  2. Linux文件系统的主要目录结构说明及分区方案

    Linux操作系统有一些固定的目录.各种Linux发行版的目录结构虽然不会一模一样,但是不会有很大差异.知道了这些目录的作用,不仅对你进行磁盘分区规划很有帮助,而且会让你以后的日常维护工作变得轻松.只 ...

  3. Linux基础系列—Linux内核源码目录结构

    /** ****************************************************************************** * @author    暴走的小 ...

  4. 【转】完美解读Linux中文件系统的目录结构

    一.前 言 接触Linux也有一段时间了,不过这几天在编译开源程序时,才发现自己对linux文件系统的目录结构了解的不够透彻,很多重要目录都说不清楚是用来干嘛的,于是在网上百度了一下这方面的介绍,根据 ...

  5. 比起 Windows,怎样解读 Linux 的文件系统与目录结构?

    Linux 和 Windows 的文件系统有些不同,在学习使用 Linux 之前,若能够了解这些不同,会有助于后续学习. 本文先对 Windows 和 Linux 上面文件系统原理.组织概念进行区分, ...

  6. linux目录结构 主流Linux发行版的目录结构

    目录 目录结构 一般教学的目录 CentOS7 openSUSE15.1 Ubuntu18.04 详细说明: /dev目录 /etc目录 /proc目录 /usr目录 /var目录 比较重要的目录 文 ...

  7. Linux系列:快捷键、目录结构、用户目录

    一.快捷键 1.历史命令 查看历史命令:history [root@centos-master ~]# history 1 2020-10-25 21:03:39 2 2020-09-17 20:43 ...

  8. Linux 文档与目录结构

    Linux之文档与目录结构   Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同.首先Linux没有“盘(C盘.D盘.E盘)”的概念.已经建立文件系统的硬盘分区被挂载到 ...

  9. Linux编程 2 (遍历目录cd与查看文件和目录ls)

    一.   遍历目录 在 linux系统上,可以使用cd切换目录命令. 分二种路径,一是绝对文件路径,另一种是相对文件路径. 1.  绝对文件路径 在虚拟目录中采用文件路径,以虚拟目录根目录开始,相当于 ...

随机推荐

  1. 你是否决绝平庸,你有勇气来学C/C++吗,有勇气来检验你是否经得起世界五百强的面试

       如果你来传智播客学习 你的目标就是要积累工作经验 有机会参加世界五百强的面试 秒杀世界五百强的面试 赢得高薪的offer! C/C++课程大纲 C语言3周21天 完全掌握C语言的本质,成为一名合 ...

  2. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  3. Openstack service default port

    Block Storage (cinder) 8776 publicurl and adminurl Compute API (nova-api) 8773 EC2 API 8774 openstac ...

  4. BeanUtils\DBUtils

    BeanUtil: 需要导入 beanutil包和logging日志包 用于给对象属性赋值. setProperty与copyProperty区别: 这个问题搁置,还不会. 将map数据拷贝到对象中, ...

  5. C++ - Vector 计算 均值(mean) 和 方差(variance)

    Vector 计算 均值(mean) 和 方差(variance) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24623187 ...

  6. DataTable 修改列名 删除列 调整列顺序

    DataTable myDt =dt;//删除列myDt.Columns.Remove("minArea");myDt.Columns.Remove("maxArea&q ...

  7. English - when用法

    一.作为副词,它有以下的用法:  1. 作为疑问副词,引导特殊疑问句,意为“什么时候:何时”.如: ( 1 ) When will they come back?( 2 ) What time wil ...

  8. 关于group by的一段SQl语句——Oracle

    select cc.fformulacode, cc.rangedate, dd.fpervalue from (select n1.fformulacode, max(n1.frangedate) ...

  9. iOS中点击背景收键盘

    这一次给大家带来的是ios中点击背景如何收键盘(感觉不错给个赞

  10. AppDelegate关于应用程序挂起、复原与终止的代理方法

    AppDelegate关于应用程序挂起.复原与终止的代理方法: 首次运行: - (BOOL)application:(UIApplication *)application didFinishLaun ...