1.6.3 Linux 文件属性函数

Linux 高并发学习笔记 - 笔记索引

  • Linux中一切皆文件,你同样可以用下面这些函数操作目录等特殊文件。
前言

关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown用起来太繁琐了。那么关于函数这一块主要也是读文档就能解决的了。

关于文档可以使用Linux命令man 2/3 xxx查看,在后面也写了查看每个文档的命令。

返回文件属性
  • statlstat

  • 常用属性:

    st_mode:文件属性编码。

    st_size:文件大小Bytes

    st_atime:访问时间。

    st_mtime:修改时间。

    st_ctime:修改时间,指属性修改。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//  get file status
//      pathname:
//          the path of target file
//      statbuf:
//          the container to restore data
//          struct stat {//              dev_t     st_dev;         /* ID of device containing file */
//              ino_t     st_ino;         /* Inode number */
//              mode_t    st_mode;        /* File type and mode */
//              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;     /* Block size for filesystem I/O */
//              blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
//
//              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
//              #define st_mtime st_mtim.tv_sec
//              #define st_ctime st_ctim.tv_sec
//          };
//      return value:
//          return 0 for success, -1 for error
int stat(const char *pathname, struct stat *statbuf);
// similar to stat
int lstat(const char *pathname, struct stat *statbuf);// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("stat");// About more
// $ man 2 stat
  • lstatstat的区别:stat在访问软链接时,返回软链接属性;lstat在访问软链接时,返回指向文件属性。
$ ln -s src.txt link.lnk  // 建立软链接 link.lnk -> src.txt
判断文件访问
  • access
#include <unistd.h>
//  check permissions to the file for current process
//      pathname:
//          the file path
//      mode:
//          optional and limitless:
//              F_OK for file exists,
//              R_OK for read permission for process,
//              W_OK for write permission for process,
//              X_OK for execute permission for process
//      return value:
//          return 0 for all permission is available, -1 for other case and error
int access(const char *pathname, int mode);// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("access");// About more
// $ man 2 access
修改文件属性
  • chmod
#include <sys/stat.h>
//  change mode of file
//      pathname:
//          the file path
//      mode:
//          three digits of oct-based number
//          UMASK work here
//      return value:
//          return 0 for success, -1 for error
int chmod(const char *pathname, mode_t mode);// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chmod");// About more
// $ man 2 chmod
修改文件所有
  • chown
#include <unistd.h>
//  change owner of file
//      pathname:
//          the file path
//      owner:
//          the UID of owner
//      group:
//          the GID of group
//      return 0 for success, -1 for error
int chown(const char *pathname, uid_t owner, gid_t group);// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chown");// About more
// $ man 2 chown

Linux 高并发学习笔记 - Linux 文件属性函数相关推荐

  1. Linux 高并发学习笔记 - Linux 文件操作函数

    1.6.2 Linux 文件操作函数 Linux 高并发学习笔记 - 笔记索引 前言 关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown用起来太繁琐了.那么关于 ...

  2. Linux 高并发学习笔记 - Linux 目录操作函数

    1.6.4 Linux 目录操作函数 Linux 高并发学习笔记 - 笔记索引 文章目录 1.6.4 Linux 目录操作函数 前言 切换工作目录 查看工作目录 创建目录 重命名目录 移除目录 遍历目 ...

  3. Linux 高并发学习笔记 - exec 函数簇重载进程

    2.2.4 exec 函数簇重载进程 Linux 高并发学习笔记 - 笔记索引 execl.execlp.execle.execv.execvp.execvpe exec函数簇将重载进程,直接覆盖当前 ...

  4. 分布式系统 概念 高可用 高并发 学习笔记

    分布式系统 概念 高可用 高并发 学习笔记 0. 分布式系统基本概念 0.1 背景 分布式系统是由一组通过网络进行通信.为了完成共同的任务而协调工作的计算机节点组成的系统.分布式系统的出现是为了用廉价 ...

  5. Java多线程高并发学习笔记(一)——ThreadRunnable

    进程与线程 首先来看百度百科关于进程的介绍: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.它不只是程序的代码,还包括当前的 ...

  6. JAVA高并发学习笔记(二) 多线程基础

    1.1什么是线程 线程是进程(程序在计算机上的一次执行活动)内的执行单元 进程是以独立于其他进程的方式运行的,进程间是互相隔离的.一个进程无法直接访问另一个进程的数据.进程的资源诸如内存和CPU时间片 ...

  7. 【高数学习笔记】1.函数、极限、连续

    文章目录 一.数列极限和函数极限的联系和区别 二.极限的复合传递 三.无穷大和无穷小性质对比 无穷大 无穷大 四.减法的重要变形思想(重中之重) 五.f(x)±g(x)(比如x-sinx)型怎么处理 ...

  8. Linux 高并发服务器开发

    该文章是通过观看牛客网的视频整理所得,以及在实践过程中遇到的问题及解决方案的整理总结. Linux 高并发服务器开发 linux 系统编程 linux 环境的搭建 环境搭建需要的软件 虚拟机中安装 u ...

  9. kali linux 渗透测试学习笔记——被动信息收集

    kali linux 渗透测试学习笔记--linux 被动信息收集 被动信息收集 被动信息收集 公开渠道可获得的信息 已公开的信息,通过互联网等渠道去获得 与目标系统不产生直接交互 不对目标访问,扫描 ...

最新文章

  1. 第 127 章 Piranha - Cluster administation tools
  2. bs4之标签树的上行遍历
  3. WebStorm使用ES6
  4. WCF,Net remoting,Web service概念(转)
  5. 任务分发系统-Qcmd-http详解
  6. 计算机鼠标显示停顿原因,经常遇到鼠标指针停顿卡的解决方法
  7. 遗传算法matlab_遗传算法 (GA) 进行多参数拟合 【MATLAB】
  8. 谷歌flutter_在Flutter中使用Google Pay Through Stripe接受付款
  9. 扫普通二维码打开小程序配置详情
  10. 人脸匹配对齐算法pytorch_PyTorch 实现孪生网络识别面部相似度
  11. 亲测~Win10开启系统自带Wifi热点步骤
  12. 100个开源游戏-街机类、棋牌类、休闲益智类、教育类、音乐类、RPG和AVG、策略类开源游戏【转】...
  13. 银行与沪深300走势对比
  14. 解决:ARouter 报错 There is no route matched
  15. IMDG中的陷阱和问题
  16. 阻止switch开关的事件冒泡
  17. 成本低廉的深度学习与渲染显卡购买方案--英伟达矿卡P106
  18. 华为手机鸿蒙系统手机_升级快讯:又一批华为手机可以升级到“鸿蒙系统”了!...
  19. 【维修】如何成功做网线?
  20. 浅谈xhr和fetch

热门文章

  1. VS导入数据到数据库
  2. python笔记14介绍几个魔法方法
  3. 利物浦VS曼城,罗指导的先手与工程师的后手
  4. h5前端基础面试题(微信小程序)
  5. VMware虚拟机中显示CPU不支持VT的解决方法
  6. c语言函数16进制变10进制,C中16进制与10进制互转
  7. 动手学深度学习---下载d2l
  8. python在excel中插入二维码
  9. 运维网关系列4:Qin Admin
  10. 深入理解SD卡基础原理以及内部结构的总结 (转)