关于Linux目录访问函数总结

简介: 关于Linux目录访问函数总结

Linux下目录访问函数总结,主要是涉及到的函数,以及所在头文件

 

获得工作目录:

#include   <unistd.h>   
 
char   *getcwd(char   *buf,size_t   size);char   *getwd(char   *buf);/*this   is   for   FreeBSD*/ 
 


改变当前目录:

#include   <unistd.h>   
 
int   chdir(const   char   *path); 


保存当前目录:

#include   <unistd.h>
 
int   fchdir(int   fd); 


建立新目录:

#include   <sys/type.h>   
#include   <sys/stat.h>   
 
int   mkdir(const   char   *path,mode_t   mode); 


删除目录:

#include   <unistd.h>   
 
int   rmdir(const   char*   path); 


打开目录进行搜索:

#include   <sys/type.h> 
#include   <dirent.h> 
 
DIR   *opendir(const   char   *pathname);   int   dirfd(DIR   *dirp); 


关闭目录:

#include   <sys/types.h> 
 
#include   <dirent.h> int   closedir(DIR   *dirp); 


搜索目录:

#include   <sys/type.h> 
#include   <dirent.h>   
 
struct   dirent   *readdir(DIR   *dirp); 


重新回到目录的开始:

#include <sys/type.h> 
#include <dirent.h> 
 
void   rewinddir(DIR   *dirp); 


保存目录中的位置:

#include   <sys/type.h> 
#include   <dirent.h> 
 
long   telldir(const   DIR   *dirp); 


在目录内恢复位置:

#include   <sys/type.h>
#include   <dirent.h> 
 
void   seekdir(DIR   *dirp,long   loc); 


扫描目录:  

#include   <sys/type.h> 
#include   <dirent.h> 
 
int   scandir(const   char   *diename,struct   dirent   ***namelist,int   (*select)(struct   dirent   *),int   (*compar)(const   void   *,const   viod*)); 


遍历目录结构:

#include   <ftw.h> 
 
int   ftw(const   char*   path,int(*fn)(const   char   *obj_path,const   struct   stat   *obj_stat,int   obj_flags),int   depth); 
 
int   nftw(const   char*   path,int(*fn)(const   char   *obj_path,const   struct   stat   *obj_stat,int   obj_flags,struct   FTW   obj_FTW),int   depth,int   flags); 


改变根目录:

#include   <unistd.h> 
 
int   chroot(const   char   *dirname);
目录
相关文章
|
2天前
|
Linux
Linux目录删除指南:彻底解决“Is a directory”错误
在 Linux 系统中遇到 `cannot remove &#39;xxx&#39;: Is a directory` 错误,是因为删除目录时未使用正确参数。解决方法包括:1) 使用 `rmdir` 删除空目录或 `rm -r` 删除非空目录;2) 检查并调整目录权限(如通过 `sudo` 提权);3) 处理特殊场景,例如文件属性异常、特殊字符或进程占用;4) 替代方法如 `find -delete` 或文件系统修复。操作前建议备份数据,并启用防误删功能(如 `alias rm=&#39;rm -i&#39;`)。掌握 `rm` 和 `rmdir` 的区别是关键。
14 1
|
1月前
|
存储 Linux
linux中的目录操作函数
本文详细介绍了Linux系统编程中常用的目录操作函数,包括创建目录、删除目录、读取目录内容、遍历目录树以及获取和修改目录属性。这些函数是进行文件系统操作的基础,通过示例代码展示了其具体用法。希望本文能帮助您更好地理解和应用这些目录操作函数,提高系统编程的效率和能力。
123 26
|
24天前
|
Linux
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
38 14
|
3月前
|
Linux
【Linux】System V信号量详解以及semget()、semctl()和semop()函数讲解
System V信号量的概念及其在Linux中的使用,包括 `semget()`、`semctl()`和 `semop()`函数的具体使用方法。通过实际代码示例,演示了如何创建、初始化和使用信号量进行进程间同步。掌握这些知识,可以有效解决多进程编程中的同步问题,提高程序的可靠性和稳定性。
124 19
|
3月前
|
Linux Android开发 开发者
linux m、mm、mmm函数和make的区别
通过理解和合理使用这些命令,可以更高效地进行项目构建和管理,特别是在复杂的 Android 开发环境中。
98 18
|
3月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
211 8
|
3月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
138 13
|
Linux 网络安全 数据安全/隐私保护
|
3天前
|
Linux
Linux 常用文件查看命令
`cat` 命令用于连接文件并打印到标准输出,适用于快速查看和合并文本文件内容。常用示例包括:`cat file1.txt` 查看单个文件,`cat file1.txt file2.txt` 合并多个文件,`cat &gt; filename` 创建新文件,`cat &gt;&gt; filename` 追加内容。`more` 和 `less` 命令用于分页查看文件,`tail` 命令则用于查看文件末尾内容,支持实时追踪日志更新,如 `tail -f file.log`。
21 5
Linux 常用文件查看命令
|
1月前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
77 24
Linux系统之whereis命令的基本使用