linux 操作目录函数,linux 文件和目录操作的相关函数使用教程

Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。其中最常用操作之一就是对文件和目录操作了,以下是对linux中文件和目录操作的相关函数进行了详细的分析介绍,需要的朋友可以过来参考下。

20281bc45061876a72b77bd0b6d8286f.png

方法步骤

struct stat

{

mode_t st_mode; 文件类型,文件权限

ino_t st_ino; i节点号

dev_t st_dev;

dev_t st_rdev; 设备文件序号

nlink_t st_nlink; 链接

uid_t st_uid;

gid_t st_gid; 用户ID

off_t st_size; 文件大小,此字段只对普通文件、目录文件和符号连接有意义。

time_t st_atime; 最后存取时间

time_t st_mtime; 文件内容的最后修改时间

time_t st_ctime; 文件状态的最后修改时间

long st_blksize;

long st_blocks;

};

1,stat函数取得文件信息。

#include

#include

int stat(const char *pathname, struct stat *buf);

int fstat (int fd,struct stat *buf);

int lstat(const char *pathname, struct stat *buf);

lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息

2,access函数判断文件权限

#include

int access (const char *name, int mode) ;

返回:若成功则为 0,若出错则为- 1

access函数的mode常数,取自

mode 说 明

R_OK 测试读许可权

W_OK 测试写许可权

X_OK 测试执行许可权

F_OK 测试文件是否存在

3,umask函数设置文件创建屏蔽字

#include

#include

mode_t umask(mode_t task) ;

返回:以前的文件方式创建屏蔽字

4,chmod函数用于修改文件的权限

#include

#include

int chmod(const char *pathname, mode_t mode);

int fchmod(int fd, mode_t mode);

两个函数返回:若成功则为 0,若出错则为- 1

5,chown函数可用于更改文件的用户 ID和组ID。

#include

#include

int chown(const char *pathname,uid_t owner,gid_t group);

int fchown(int fd, uid_t owner, gid_t group);

int lchown(const char *pathname, uid_t owner, gid_t group);

三个函数返回:若成功则为 0,若出错则为- 1

6,在文件末尾处截短文件可以调用函数 truncate和ftruncate。将一个文件的长度截短为 0是一个特例,用O_TRUNC标志可以做到这一点。

#include

#include

int truncate(const char *pathname, off_t

length) ;

int ftruncate(int filedes, off_t length) ;

两个函数返回;若成功则为 0,若出错则为- 1

7,创建一个向现存文件连接的方法是使用link函数,想当于硬连接 ln。只有超级用户进程可以创建指向一个目录的新连接。其理由是这样做可能在文件系统中形成循环,大多数处理文件系统的公用程序都不能处理这种情况

#include

int link(const char*oldpath, const char *newpath) ;

返回:若成功则为 0,若出错则为- 1

为了删除一个现存的目录项,可以调用 unlink函数。

#include

int unlink(const char *pathname) ;

返回:若成功则为 0,若出错则为-1。此函数删除目录项,并将由 pathname所引用的文件的连接计数减 1。

硬连接的一些限制: ( a )硬连接通常要求连接和文件位于同一文件系统中, ( b )只有超级用户才能创建到目录的硬连接。

symlink函数创建一个符号连接。相当于软连接,ln -s

#include

int symlink(const char *oldpath, const char *sympath) ;

返回:若成功则为 0,若出错则为- 1

因为open函数跟随符号连接,所以需要有一种方法打开该连接本身,并读该连接中的名字。

readlink函数提供了这种功能。

#include

int readlink(const char *pathname, char *buf, int bufsize) ;

返回:若成功则为读的字节数,若出错则为- 1

此函数组合了open, read和close的所有操作。

8,用mkdir函数创建目录,用 rmdir函数删除目录。

#include

#include

int mkdir(const char *pathname, mode_t mode) ;

返回:若成功则为 0,若出错则为- 1

#include

int rmdir(const char *pathname) ;

返回:若成功则为 0,若出错则为 - 1

9,remove函数解除对一个文件或目录的连接。对于文件, remove的功能与unlink相同。对于目录, remove的功能与rmdir相同。

#include

int remove(const char *pathname) ;

返回:若成功则为 0,若出错则为- 1

文件或目录用rename函数更名。

#include

int rename(const char *oldname, const char *newwname) ;

返回:若成功则为 0,若出错则为- 1

10,一个文件的存取和修改时间可以用 utime函数更改。

#include

#include

int utime (const char *name, const struct utimebuf *t);

返回:若成功则为 0,若出错则为- 1

如果times是一个空指针,则存取时间和修改时间两者都设置为当前时间;

如果times是非空指针,则存取时间和修改时间被设置为 times所指向的结构中的值。此时,进程的有效用户ID必须等于该文件的所有者 ID,或者进程必须是一个超级用户进程。对文件只具有写许可权是不够的

此函数所使用的结构是:

struct utimbuf {

time_t actime; /*access time*/

time_t modtime; /*modification time*/

}

11,对文件目录的操作函数,opendir readdir rewinddir

#include

#include

DIR *opendir(const char *pathname) ;

返回:若成功则为指针,若出错则为 NULL

struct dirent *readdir(DIR *dr);

返回:若成功则为指针,若在目录尾或出错则为 NULL

void rewinddir(DIR *dr);

重置读取目录的位置为开头

int close(DIR *dr); 返回:若成功则为 0,若出错则为- 1

定义在头文件中的dirent结构与实现有关。 此结构至少包含下列两个成员:

struct dirent {

ino_t d_ino;

char d_name[NAME_MAX+1];

}

12,chdir,改变当前目录

#include

int chdir(const char *pathname);

int pchdir(int fd);

getcwd,得到当前目录的完整路径.

#include

char *getcwd(char *buf, size_t size);

若失败返回NULL, buf为存储路径的字符数组,size为长度

补充:Linux基本命令

①ls 意为list 列出当前文件夹中的文件

-l 显示文件的属性 可用ll来表示

②alias 别名 看看是否有别名的文件

③cd dir 跳跃目录 -P选项 将路径中的链接文件替换成链接指向的文件路径

④pwd 查看当前工作的文件夹名 使用-P的选项,会直接进入到其中,相当于cd

相关阅读:Linuxshell脚本不执行问题实例分析

shell脚本不执行问题:某天研发某同事找我说帮他看看他写的shell脚本,死活不执行,报错。我看了下,脚本很简单,也没有常规性的错误,报“:badinterpreter:Nosuchfileordirectory”错。看这错,我就问他是不是在windows下编写的脚本,然后在上传到linux服务器的……果然。原因:在DOS/windows里,文本文件的换行符为rn,而在*nix系统里则为n,所以DOS/Windows里编辑过的文本文件到了*nix里,每一行都多了个^M。解决:

1)重新在linux下编写脚本;

2)vi:%s/r//g:%s/^M//g(^M输入用Ctrl+v,Ctrl+m)附:sh-x脚本文件名,可以单步执行并回显结果,有助于排查复杂脚本问题。

linux 文件和目录操作的相关函数相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值