创建和删除

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

int rmdir(const char *pathname); 另外remove也可以删除文件夹

mode: S_IRUSR,S_IWUSR,S_IXUSR,S_IRGRP,S_IWGRP,S_IXGRP,S_IROTH,S_IWOTH,S_XOTH

S_IRWXU,S_IRWXG,S_IRWXO

char *pathname="./newfolder";
if(mkdir(pathname,S_IRWXU|S_IRGRP|S_IWGRP|S_IROTH)<0){
perror("mkdir failed");
exit(1);
}else
printf("mkdir success");
if(rmdir(pathname)<0){
perror("rmdir failed");
exit(1);
}else
printf("rmdir success");

打开,读取,关闭

目录以一个DIR来表示, 有点类似FILE, 读FILE返回char, 读目录返回dirent结构

dirent结构有两个重要属性:

ino_t d_ino; // i节点信息, 文件或文件属性

char d_name[]; //文件或文件夹名称

打开文件夹: DIR *opendir(pathname)

关闭文件夹: int closedir(pathname)

读取文件夹: struct dirent *readdir(DIR *dp)

// #include <dirent.h>
DIR *curdir;
char *path=".";
if((curdir=opendir(path)) == NULL){
perror("opendir failed");
exit(1);
}else
printf("opendir success\n"); struct dirent *dirp;
while((dirp=readdir(curdir)) != NULL)
printf("%s\n",dirp->d_name); if(closedir(curdir) <0){
perror("closedir failed");
exit(1);
}else
printf("closedir success\n");

切换目录

获取当前目录名称: char *getcwd(char *buf, size_t size);

切换目录: int char(const char *pathname);

char buf[256];
if(getcwd(buf,256) == NULL){
perror("getcwd failed");
exit(1);
}else
printf("current dir : %s\n", buf); char *parent="..";
if(chdir(parent) <0){
perror("chdir failed");
exit(1);
}else
printf("current dir: %s\n",getcwd(buf,256));

遍历文件夹例子

注意点:

  • 进入子目录时用到chdir(childpath)
  • 子目录遍历后要返回父目录chdir("..")
  • printf("%*s",5,char* s)=prinf("%5s,char *s)

    表示打印s时占据5个字母的宽度,超过设定的值时全部打印
// #include <dirent.h>
DIR *curdir;
char *path=".";
if((curdir=opendir(path)) == NULL){
perror("opendir failed");
exit(1);
}else
printf("opendir success\n"); struct dirent *dirp;
while((dirp=readdir(curdir)) != NULL)
printf("%s\n",dirp->d_name); if(closedir(curdir) <0){
perror("closedir failed");
exit(1);
}else
printf("closedir success\n");

切换目录

获取当前目录名称: char *getcwd(char *buf, size_t size);

切换目录: int char(const char *pathname);

char buf[256];
if(getcwd(buf,256) == NULL){
perror("getcwd failed");
exit(1);
}else
printf("current dir : %s\n", buf); char *parent="..";
if(chdir(parent) <0){
perror("chdir failed");
exit(1);
}else
printf("current dir: %s\n",getcwd(buf,256));

遍历文件夹例子

注意点:

  • 进入子目录时用到chdir(childpath)
  • 子目录遍历后要返回父目录chdir("..")
  • printf("%*s",5,char* s)=prinf("%5s,char *s)

    表示打印s时占据5个字母的宽度,超过设定的值时全部打印
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h> void err_quit(const char *str){
perror(str);
exit(1);
} void scan_dir(char *dir, int depth){ //depth用于缩进
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL)
err_quit("opendir failed"); if(chdir(dir) <0) //切换目录, 切换到子目录
err_quit("chdir failed"); 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); // 输出目录名称
scan_dir(entry->d_name, depth+4); // 递归调用自身,扫描下一级目录的内容
}else
printf("%*s%s\n", depth, "", entry->d_name); //如果不是目录就只打印文件名
} if(chdir("..") <0) // 回到上级目录
err_quit("chdir failed");
if(closedir(dp) <0) //关闭目录
err_quit("closedir failed");
} int main(){
puts("scan /home:");
scan_dir("/root", 0);
puts("scan over.");
return 0;
}

linux 目录的更多相关文章

  1. 每天一个linux命令(23):Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  2. 79 umount-卸除目前挂在Linux目录中的文件系统

    Linux umount命令用于卸除文件系统. umount可卸除目前挂在Linux目录中的文件系统. 语法 umount [-ahnrvV][-t <文件系统类型>][文件系统] 参数: ...

  3. 详解Linux目录(目录树详细解释)

    给大家一篇关于Linux目录 方面的详细说明,好好读一下! Linux目录详解(RHEL5.4) linux有四种基本文件系统类型:--普通文件:如文本文件.c语言源代码.shell脚本等,可以用ca ...

  4. Linux 目录配置

    一.Linux 目录配置标准:FHS FHS(Filessystem Hierarchy Standard) 的重点在于规范每个特定的目录下应该要放置什么样子的数据. FHS依据文件系统使用的频繁与否 ...

  5. linux 目录下文件批量植入和删除,按日期打包

    linux目录下文件批量植入 [root@greymouster http2]# find /usr/local/http2/htdocs/ -type f|xargs sed -i "   ...

  6. day 2 Linux目录结构

    Linux系统的目录结构的基本介绍: 1)在逻辑上的所有目录(包括目录下的子目录)都在最高级别的目录“/”下. 根(/)目录是Linux系统中所有目录的起始点(顶点),根下面的目录及子目录是一个有层次 ...

  7. 【Linux】Linux 目录结构

    博客已转移,请借一步说话 .http://www.weixuehao.com/archives/492 初学Linux,首先需要弄清Linux 标准目录结构 / root --- 启动Linux时使用 ...

  8. CentOS6.5菜鸟之旅:纯转载Linux目录结构

    来自:http://www.iteye.com/topic/1125162 使用linux也有一年多时间了  最近也是一直在维护网站系统主机  下面是linux目录结构说明 本人使用的是centos系 ...

  9. 每天一个linux命令(19):Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  10. Linux目录结构及常用命令(转载)

    一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...

随机推荐

  1. Oracle基础 物理备份 冷备份和热备份(转)

    一.冷备份介绍:    冷备份数据库是将数据库关闭之后备份所有的关键性文件包括数据文件.控制文件.联机REDO LOG文件,将其拷贝到另外的位置.此外冷备份也可以包含对参数文件和口令文件的备份,但是这 ...

  2. JSONP跨域原理和jQuery.getJSON用法

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式).本文主要介绍JS ...

  3. BI跟报表一样吗?

    一.报表≠BI很多的人认为,报表就是BI.实际上,报表只是BI的一部分,虽然BI应用的结果通常需要通过报表来展示,但是,BI绝对不仅仅是报表.其实,大家对这些概念的理解,如同15年前的ERP一样.19 ...

  4. [转]在PHP语言中使用JSON

    本文转自:http://www.ruanyifeng.com/blog/2011/01/json_in_php.html 作者: 阮一峰 日期: 2011年1月14日 目前,JSON已经成为最流行的数 ...

  5. (ASP.NET)总结MVC中@Html表单用法

    1.当type类型是text时:@Html.TextBoxFor(model => Model.Name,new{@style = "width: 50px;", @clas ...

  6. Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结

    一.Math.round() 作用:四舍五入返回整数.(返回参数+0.5后,向下取整) Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round( ...

  7. 微信小程序个人理解

    1:小程序不是用HTML5开发,它是由微信全新定义的规范,是基于XML+JS的,不支持也不兼容HTML,兼容受限的部分CSS写法.(wxml) weixin markup language 2:小程序 ...

  8. java使用BufferedImage和Graphics实现图片合成

    package com.igoxin.weixin.custom; import java.awt.Graphics; import java.awt.image.BufferedImage; imp ...

  9. LLVM language 参考手册 翻译停止相关

    再翻译LLVM language 参考手册的时候,个人感觉很多东西都不是很懂,因此打算学习完编译原理后再去继续研究翻译,多有不便望见谅

  10. unity打包android游戏部分问题总结

    一:虚拟导航栏挡到游戏按钮: 解决方案如下: 1.获取焦点的时候隐藏 虚拟导航条 Navigation bar 隐藏导航条 2.出现导航条的时候,改变游戏界面大小 Unity tidbits: cha ...