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中,或 ...
随机推荐
- windows下mongoengine报错False is not a read preference.的解决办法
mongoengine是基于pymongo的,后者的3.0版本在windows下似乎会有这个问题,解决方法就是降级: pip install pymongo==2.8 来源:upgrade to py ...
- Oracle基础(五) 权限管理
一.数据库权限分类 权限是指用户对某个功能是否具有执行的权利.在Oracle中,权限分为系统权限和对象权限. 1.系统权限 系统规定用户使用数据库的权限.例如创建表空间,创建表,删除表的权利.这些都属 ...
- linux初学 :linux 常用命令(一)
首先,是关机/重启命令,仅在虚拟机时使用,实际工作中用不到 reboot 一般不跟参数使用,输入指令即可重启 shutdown 一般需要跟参数,例: shutdown -r 延时多少分钟重启,一般使用 ...
- nginx 反向代理设置
假设对本机80端口的访问为一台服务器,对本机8000端口的访问为另一台服务器,下面这样写一个nginx的反向代理配置,就可以实现所有请求都转移 server {listen 0.0.0.0:80;se ...
- Pascal 语言中字符与字符串
[题目]输入一段文章(255个字符以内),求文章中单词的个数,相同单词只记一次,The 和 the 视作相同. [敲代码] //网友代码 var article,w:string; arr:array ...
- rfc 标准文档目录
1. xmpp的文档 (3920版本) https://tools.ietf.org/html/rfc3920 2. MQTT 3. SIP
- HTML中的英文缩写标记、属性
我们知道是表示一个空格,因为在HTML中手动敲多个空格浏览器会认为是一个空格,具体的英文全称:Non Breaking Spacing 不间断空格
- 为IE单独写CSS的三种方法
本文由 Kayo Lee 发表,本文链接:http://kayosite.com/the-methods-make-css-only-for-ie.html 因为万恶的 IE 存在各种的不标准,因此, ...
- 遇到的 autoresizingMask 相关的问题
1.前言 当一个控件设置好 frame,然后出现会 frame 显示不准或是跟随父控件的变化而变化了,你就要考虑是否是 autoresizing 的问题了 当在 xib 中布局时,报 NSAutore ...
- OC3_协议关键字
// // Student.h // OC3_协议关键字 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangx ...