linux 目录
创建和删除
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 目录的更多相关文章
- 每天一个linux命令(23):Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- 79 umount-卸除目前挂在Linux目录中的文件系统
Linux umount命令用于卸除文件系统. umount可卸除目前挂在Linux目录中的文件系统. 语法 umount [-ahnrvV][-t <文件系统类型>][文件系统] 参数: ...
- 详解Linux目录(目录树详细解释)
给大家一篇关于Linux目录 方面的详细说明,好好读一下! Linux目录详解(RHEL5.4) linux有四种基本文件系统类型:--普通文件:如文本文件.c语言源代码.shell脚本等,可以用ca ...
- Linux 目录配置
一.Linux 目录配置标准:FHS FHS(Filessystem Hierarchy Standard) 的重点在于规范每个特定的目录下应该要放置什么样子的数据. FHS依据文件系统使用的频繁与否 ...
- linux 目录下文件批量植入和删除,按日期打包
linux目录下文件批量植入 [root@greymouster http2]# find /usr/local/http2/htdocs/ -type f|xargs sed -i " ...
- day 2 Linux目录结构
Linux系统的目录结构的基本介绍: 1)在逻辑上的所有目录(包括目录下的子目录)都在最高级别的目录“/”下. 根(/)目录是Linux系统中所有目录的起始点(顶点),根下面的目录及子目录是一个有层次 ...
- 【Linux】Linux 目录结构
博客已转移,请借一步说话 .http://www.weixuehao.com/archives/492 初学Linux,首先需要弄清Linux 标准目录结构 / root --- 启动Linux时使用 ...
- CentOS6.5菜鸟之旅:纯转载Linux目录结构
来自:http://www.iteye.com/topic/1125162 使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系 ...
- 每天一个linux命令(19):Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- Linux目录结构及常用命令(转载)
一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...
随机推荐
- Ubuntu16.04安装VMware Tools问题
*************************************************************************** 问题:客户机操作系统已将 CD-ROM 门锁定, ...
- 简单介绍AngularJs Filters
网站链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angular-filters/ Filter作用就是接收一个输入,通过某 ...
- POJ 1502 MPI Maelstrom (最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6044 Accepted: 3761 Des ...
- Storm累计求和中使用各种分组Grouping
Shuffle Grouping: 随机分组, 随机派发stream里面的tuple, 保证bolt中的每个任务接收到的tuple数目相同.(它能实现较好的负载均衡) Fields Grouping: ...
- hdu 4055 动态规划
#include<map> #include<set> #include<cmath> #include<queue> #include<cstd ...
- macOS10.12允许所有来源设置
如何调出允许所有来源呢? 很简单一行命令搞定 调出允许所有来源 1.打开终端执行命令 sudo spctl --master-disable 2.你在打开偏好设置--> 安全与隐私 好了赶快 ...
- 基础面试题——Javascript
1.介绍js的基本数据类型 Undefined.Null.Boolean.Number.String 2.js有哪些内置对象? 数据封装类对象:Object.Array.Boolean.Number ...
- CSS之全屏背景图
吐槽啦:Yeah 明天就是国庆了o(* ̄▽ ̄*)o!哈哈,提前祝福各位园友国庆快乐.假期愉快.生活美满.天天开心!国庆我要回家一趟,把一些不用的东西带回家,走访一下亲朋好友,在家打几天酱油~~~ 言 ...
- Swiper之滑块1
之前介绍过Swiper,它是一个神奇的插件.类似于Android的触屏操作,Swiper应用于Web中也可以实现这样的效果,我们来看看(用鼠标可拖动). startSlide Integer (def ...
- Jquery AJax Post 返回值问题
var msg=0; Validater('abc'); function Validater(Name) { var itemId = 1; $.ajax({ url: 'adminmenu/Val ...