linux 目录

时间:2023-04-18 11:30:38

创建和删除

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;
}