(十四)文件操作——stat命令与函数

来源:互联网 发布:淘宝云客服培训网址 编辑:程序博客网 时间:2024/04/28 16:07

stat命令


stat既有命令也有同名函数,用来获取文件Inode里主要信息,所以stat命令的输出信息比ls命令的输出信息要更详细,stat 跟踪符号链接,lstat不跟踪符号链接,其中会输出对应文件的文件名(File)、文件大小(Size)、占用物理扇区数(Blocks)、系统块大小(IO Block)、文件类型(前一篇博客中介绍的)、设备的主编号和次编号(Device做驱动程序时经常用到,在系统层面几乎用不到吧)、文件索引号(Inode文件节点)、硬链接个数(Links)、权限(Access)、用户id(Uid)、组id(Gid)、还有三个重要的时间。

stat里面时间辨析:

  • atime(最近访问时间access time): 最近打开文件的时间,读一次这个文件的内容,这个时间就会更新。比如对这个文件运用 more、cat等命令。但是ls、stat命令都不会修改文件的访问时间(在上一篇博客中已经介绍原因)。
  • mtime(最近更改时间modifiy time): 指最近修改文件内容的时间 ,修改时间,修改时间是文件内容最后一次被修改时间。比如:vi后保存文件。ls -l列出的时间就是这个时间。
  • ctime(最近改动时间change time): 指最近改动Inode的时间,状态改动时间。是该文件的i节点最后一次被修改的时间,通过chmod、chown命令修改一次文件属性,这个时间就会更新。
    这里写图片描述

stat函数

头文件:#include < sys/stat.h> #include < unistd.h>

定义函数:int stat(const char * file_name, struct stat *buf);

函数说明:stat()用来将参数file_name 所指的文件状态, 复制到参数buf 所指的结构中。

#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>int stat(const char *path, struct stat *buf);int fstat(int fd, struct stat *buf);int lstat(const char *path, struct stat *buf);struct stat {dev_t st_dev; /* ID of device containing file 文件的设备编号*/ino_t st_ino; /* inode number 文件索引号(Inode文件节点)*/mode_t st_mode; /* protection 文件的类型和存取的权限*/nlink_t st_nlink; /* number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.*/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 file system I/O 文件系统的I/O 缓冲区大小*/blkcnt_t st_blocks; /* number of 512B blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节*/time_t st_atime; /* time of last access 文件最近一次被存取或被执行的时间*/time_t st_mtime; /* time of last modification 文件最后一次被修改的时间*/time_t st_ctime; /* time of last status change 最近一次被更改的时间*/};


举例:

#include <sys/types.h>#include <sys/stat.h>#include <time.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    struct stat sb;    //参数错误    if (argc != 2) {        fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);        exit(EXIT_FAILURE);    }    //stat函数调用失败    if (stat(argv[1], &sb) == -1) {        perror("stat");        exit(EXIT_FAILURE);    }    //打印文件类型,可以查看man手册知道S_IFMT和case后面的内容    printf("File type:                ");    switch (sb.st_mode & S_IFMT) {        case S_IFBLK:  printf("block device\n");            break;        case S_IFCHR:  printf("character device\n");        break;        case S_IFDIR:  printf("directory\n");               break;        case S_IFIFO:  printf("FIFO/pipe\n");               break;        case S_IFLNK:  printf("symlink\n");                 break;        case S_IFREG:  printf("regular file\n");            break;        case S_IFSOCK: printf("socket\n");                  break;        default:       printf("unknown?\n");                break;    }    //打印文件节点(索引号)    printf("I-node number:            %ld\n", (long) sb.st_ino);    //打印文件类型号    printf("Mode:                     %lo (octal)\n",            (unsigned long) sb.st_mode);    //打印硬链接个数    printf("Link count:               %ld\n", (long) sb.st_nlink);    //打印用户ID (UID)  和  所在组ID (GID)    printf("Ownership:                UID=%ld   GID=%ld\n",            (long) sb.st_uid, (long) sb.st_gid);    //打印一个块的大小    printf("Preferred I/O block size: %ld bytes\n",            (long) sb.st_blksize);    //打印文件大小    printf("File size:                %lld bytes\n",            (long long) sb.st_size);    //打印所用物理扇区大小(单位是512B)    printf("Blocks allocated:         %lld\n",            (long long) sb.st_blocks);    printf("Last status change:       %s", ctime(&sb.st_ctime));    printf("Last file access:         %s", ctime(&sb.st_atime));    printf("Last file modification:   %s", ctime(&sb.st_mtime));    exit(EXIT_SUCCESS);}

运行结果如下:

$./app a.c File type:                regular fileI-node number:            2898399Mode:                     100664 (octal)Link count:               1Ownership:                UID=1000   GID=1000Preferred I/O block size: 4096 bytesFile size:                2007 bytesBlocks allocated:         8Last status change:       Wed Feb  3 03:01:25 2016Last file access:         Fri Dec 30 21:34:50 2016Last file modification:   Wed Feb  3 03:01:25 2016
5 0