获取文件属性

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> int stat(const char *pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *pathname, struct stat *buf); #include <fcntl.h>
#include <sys/stat.h> int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags);
返回值:成功0,出错-1

stat结构:

struct stat {
  dev_t st_dev; /* ID of device containing file */
  ino_t st_ino; /* inode number */
  mode_t st_mode; /* protection */
  nlink_t st_nlink; /* number of hard links */
  uid_t st_uid; /* user ID of owner */
  gid_t st_gid; /* group ID of owner */
  dev_t st_rdev; /* device ID (if special file) */
  off_t st_size; /* total size, in bytes */
  blksize_t st_blksize; /* blocksize for filesystem I/O */
  blkcnt_t st_blocks; /* number of 512B blocks allocated */   /* Since Linux 2.6, the kernel supports nanosecond
    precision for the following timestamp fields.
    For the details before Linux 2.6, see NOTES. */

  struct timespec st_atim; /* time of last access */
  struct timespec st_mtim; /* time of last modification */
  struct timespec st_ctim; /* time of last status change */   #define st_atime st_atim.tv_sec /* Backward compatibility */
  #define st_mtime st_mtim.tv_sec
  #define st_ctime st_ctim.tv_sec
};

按实际用户ID和实际组ID进行访问权限测试

#include <unistd.h>

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

#include <fcntl.h>
#include <unistd.h> int faccessat(int dirfd, const char *pathname, int mode, int flags);
返回值:成功0,出错-1

为进程设置文件模式创建屏蔽字

#include <sys/types.h>
#include <sys/stat.h> mode_t umask(mode_t mask);
返回值:之前文件模式创建屏蔽字

更改现有文件的访问权限

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode); #include <fcntl.h>
#inlcude <sys/stat.h> int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
返回值:成功0,出错-1

用于改变文件的用户ID和组ID。如果两个参数owner或group中的任意一个是-1,则对应ID不变

#include <unistd.h>

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); #include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h> int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);
返回值:成功0,出错-1

截断文件

#include <unistd.h>
#include <sys/types.h> int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
返回值:成功0,出错-

创建一个指向现有文件的链接

#include <unistd.h>

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

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h> int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
返回值:成功0,出错-

删除一个现有目录项

#include <unistd.h>

int unlink(const char *pathname);

#include <fcntl.h>
#include <unistd.h> int unlinkat(int dirfd, const char *pathname, int flags);
返回值:成功0,出错-1

解除对一个文件或目录的链接

#include <stdio.h>

int remove(const char *pathname);
返回值:成功0,出错-

对文件或目录进行重命名

#include <stdio.h>

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

#include <fcntl.h>
#include <stdio.h> int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags);
返回值:成功0,出错-

创建一个符号链接

#include <unistd.h>

int symlink(const char *target, const char *linkpath);

#include <fcntl.h>
#include <unistd.h> int symlinkat(const char *target, int newdirfd, const char *linkpath);
返回值:成功0,出错-

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

#include <unistd.h>

ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h> ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
返回值:成功读取字节数,出错-

一个文件的访问和修改时间可以用以下几个函数更改。futimens和utimensat函数可以指定纳秒级精度的时间戳。

用到数据结构是与stat函数族相同的timespec结构。

#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h> int utimensat(int dirfd, const char *pathname, const struct timespec times[], int flags); int futimens(int fd, const struct timespec times[]);
返回值:成功0,出错-

创建目录,删除目录

#include <sys/stat.h>
#include <sys/types.h> int mkdir(const char *pathname, mode_t mode); #include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h> int mkdirat(int dirfd, const char *pathname, mode_t mode);
返回值:成功0,出错-1

删除一个空目录,空目录只包含.和..

#include <unistd.h>

int rmdir(const char *pathname);
返回值:成功0,出错-

读目录

#include <sys/types.h>
#include <dirent.h> DIR *opendir(const char *name);
DIR *fdopendir(int fd);
返回值:成功指针,出错NULL
struct dirent *readdir(DIR *dirp);
返回值:成功指针,若在目录或出错,返回NULL
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);
返回值:成功0,出错-1
long telldir(DIR *dirp);
返回值:dp关联的目录中的当前位置
void seekdir(DIR *dirp, long loc);

dirent结构

struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all filesystem types */
char d_name[]; /* filename */
};

更改当前目录

#include <unistd.h>

int chdir(const char *path);
int fchdri(int fd);
返回值:成功0,出错-1

得到当前工作目录完整绝对路径名

#include <unistd.h>

char *getcwd(char *buf, size_t size);
返回值:成功返回buf,出错NULL

apue 第4章 文件和目录的更多相关文章

  1. APUE第4章 文件和目录

    4.2 文件函数 #include <sys/stat.h> int stat(const char *restrict pathname, struct stat *restrict b ...

  2. 零基础学Python--------第10章 文件及目录操作

    第10章 文件及目录操作 10.1 基本文件操作 在Python中,内置了文件(File)对象.在使用文件对象时,首先需要通过内置的open() 方法创建一个文件对象,然后通过对象提供的方法进行一些基 ...

  3. apue学习笔记(第四章 文件和目录)

    本章将描述文件系统的其他特性和文件的性质. 函数stat.fstat.fstatat和lstat #include <sys/stat.h> int stat(const char *re ...

  4. 《UNIX环境高级编程》(APUE) 笔记第四章 - 文件和目录

    4 - 文件和目录 1. 函数 stat.fstat.fstatat 和 lstat #inlcude <sys/stat.h> int stat(const char *restrict ...

  5. UNIX环境高级编程 第4章 文件和目录

    第三章说明了关于文件I/O的基本函数,主要是针对普通regular类型文件.本章描述文件的属性,除了regular文件还有其他类型的文件. 函数stat.fstat.fstatat和lstat sta ...

  6. APUE(4)---文件和目录 (3)

    十三.函数rename和renameat #include <stdio.h> int rename(const char *oldname, const char *newname); ...

  7. APUE(4)---文件和目录 (2)

    七.函数umask umask函数为进程设置文件模式创建屏蔽字,并返回之前的值,这是少数几个没有出错返回函数中的一个.其中cmask是9个常量(S_IR/W/XUSR.S_IR/W/XGRP.S_IR ...

  8. APUE学习笔记3——文件和目录

    1 简介 之前学习了执行I/O操作的基本函数,主要是围绕普通文件I/O的打开.读或写.下面继续学习Unix文件系统的其他特征和文件的基本性质.我们将从stat函数开始,了解stat结构所代表的文件属性 ...

  9. Linux命令应用大词典-第 15章 文件、目录权限和属性

    15.1 chmod:更改文件和目录的模式 15.2 chown:更改文件和目录的用户所有者和组群所有者 15.3 chgrp:更改文件或目录的所属组 15.4 umask:显示和设置文件及目录创建默 ...

随机推荐

  1. spring-boot的helloWorld详解

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...

  2. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  3. 【HDOJ6599】I Love Palindrome String(PAM,manacher)

    题意:给出一个由小写字母组成的长为n的字符串S,定义他的子串[L,R]为周驿东串当且仅当[L,R]为回文串且[L,(L+R)/2]为回文串 求i=[1,n] 所有长度为i的周驿东串的个数 n<= ...

  4. Toposort(拓扑排序)dfs递归模板

    最近刷了几题拓扑排序的题,记录一下拓扑排序 在有向图中,并且按照一定的规则(题目所给的规则)排序.如果图中出现了有向环的话就无法排序了. int gap[maxn][maxn];//记录下有向边 in ...

  5. qbxt Day2 on 19-7-25

    qbxt Day2 on 19-7-25 --TGZCBY 上午 1. 矩阵乘法在图论上的应用 有的时候图论的转移方程可以用dp的方式转移 特别是两个数的乘积求和的时候 比如邻接矩阵中f[i][j]表 ...

  6. ANSI转义代码(ANSI escape code)

    ANSI escape code - Wikipedia linux 输出绿色的✓TRUE,红色的✗FALSE : echo -e "\x1B[1;32m✓TRUE \x1B[0mXXX&q ...

  7. /proc/interrupts /proc/stat 查看中断信息

    /proc/interrupts列出当前所以系统注册的中断,记录中断号,中断发生次数,中断设备名称 如下图:从左至右:中断号   中断次数  中断设备名称 从上图可知中断号为19的arch_timer ...

  8. 聚合函数:sum,count,max,avg

    聚合函数:sum,count,max,avg等,一般作用于多条记录上.通过group by可以将数据对属于一组的数据起作用. SELECT region, SUM(population), SUM(a ...

  9. zabbix真的很简单 (安装篇)

    系统环境: Centos 6.4 一直觉得 zabbix 很简单,但是还是有好多人看了好多文档都搞不明白怎么用,我从2013年使用到现在也小有心得,如果时间允许,很高兴与大家一起分享我在使用过程中的一 ...

  10. Appium移动端自动化:Api接口详解

    滑动操作与拖拽操作 # 滚动处理 # elementObj1 目标滚动元素,elementObj2 起始滚动元素 # 底层通过action操作,与web ui相反,origin_el为目标元素,des ...