创建和删除

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. unity中js脚本与c#脚本互相调用

    unity中js脚本与c#脚本互相调用   test1.js function OnGUI() { if(GUI.Button(Rect(25,25,100,30),"JS Call CS& ...

  2. hdu-5681 zxa and wifi(dp)

    题目链接: zxa and wifi Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  3. List<T>.Sort() 排序的用法

    List<T> 可以通过 .Sort()进行排序,但是当 T 对象为自定义类型时(比如自定义模型),就需要 IComparable接口重写其中的方法来实现,实现代码如下: class Pr ...

  4. Ajax学习笔记(一)

    来源:http://www.imooc.com/learn/250 Ajax不是某种编程语言,而是一种在无须重新加载整个网页的情况下能够更新部分网页的技术. 一.概念介绍--异步 利用XMLHttpR ...

  5. ASP.Net 验证控件 RegularExpressionValidator

    定义和用法 RegularExpressionValidator 控件用于验证输入值是否匹配指定的模式. 注释:除非浏览器不支持客户端验证或 EnableClientScript 属性被设置为 fal ...

  6. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

  7. Page.ClientScript.RegisterStartupScript

    Page.ClientScript.RegisterStartupScript方法最初可用的选项之一就是使用一个可实现此功能的 .NET 类来注册脚本块. 第一个是 RegisterStartupSc ...

  8. html2canvas 踩坑总结

    需求:将html表格导出为图片,表格可以自己编辑数据,并适配各种屏幕大小.上网搜了下,找到了html2canvas,一开始使用的是最新版0.5.0,最终因为需要支持自定义div编辑框自动换行选择了v0 ...

  9. C# 前台线程和后台线程

    进程会等待所有的前台线程完成后在结束工作,但是如果只剩下后台线程,则会直接结束工作 using System; using System.Collections.Generic; using Syst ...

  10. JavaScript之放大镜效果

    在网上也浏览过许多关于JavaScript放大镜效果的文章,有的代码解释得些隐晦难懂,看的我头有点晕晕的╮(╯﹏╰)╭,我的心情是这样的: 吐槽完了,我们动动小鼠标,当鼠标经过下面这张美女图片时就实现 ...