1.file命令
file 文件名 [参数]
参数:
-b 只显示文件类型和文件编码,不显示文件名
-I 显示文件的MIME类型
-F 设置输出字符串的分隔符
-L 查看软链接文件自身文件属性
2.stat命令
stat [参数] 文件或目录名
参数:
-f 不显示文件本身的信息,显示文件所在文件系统的信息
-L 查看软链接文件关联的文件属性信息
-c 查看文件某个单个的属性信息
-t 简洁模式,只显示摘要信息,不显示属性描述
3.stat/lstat函数
stat/lstat函数功能和stat命令功能一样,只是应用场景不同。区别在于处理链接文件的方式上:
- lstat():得到的是软连接文件本身的属性信息
- stat():得到的是软连接文件关联的文件的属性信息
函数原型:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
- pathname:文件名
- buf:传输参数,文件的信息被写入到这块内存中
- 返回值:成功0,失败-1
结构体原型:
struct stat {
dev_t st_dev; // 文件的设备编号
ino_t st_ino; // inode节点
mode_t st_mode; // 文件的类型和存取的权限, 16位整形数 -> 常用
nlink_t st_nlink; // 连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; // 用户ID
gid_t st_gid; // 组ID
dev_t st_rdev; // (设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; // 文件字节数(文件大小) --> 常用
blksize_t st_blksize; // 块大小(文件系统的I/O 缓冲区大小)
blkcnt_t st_blocks; // block的块数
time_t st_atime; // 最后一次访问时间
time_t st_mtime; // 最后一次修改时间(文件内容)
time_t st_ctime; // 最后一次改变时间(指属性)
};
3.1获取文件大小
1. #include <sys/stat.h>
2.
3. int main()
4. {
5. // 1. 定义结构体, 存储文件信息
6. struct stat myst;
7. // 2. 获取文件属性 english.txt
8. int ret = stat("./english.txt", &myst);
9. if(ret == -1)
10. {
11. perror("stat");
12. return -1;
13. }
14.
15. printf("文件大小: %d\n", (int)myst.st_size);
16.
17. return 0;
18. }
3.2获取文件最后修改时间
1. //文件最后修改时间
2. #include <stdio.h>
3. #include <sys/stat.h>
4. #include <unistd.h>
5. #include <time.h>
6.
7.
8.
9. int main()
10. {
11. struct stat file_stat;
12. if(stat("add.c", &file_stat) == -1)
13. {
14. perror("stat");
15. return -1;
16. }
17.
18. struct tm *last_modify_time = localtime(&file_stat.st_mtime);
19. printf("last mofidy time: %d-%02d-%2d %02d:%02d:%02d\n",
20. last_modify_time->tm_year+1900,
21. last_modify_time->tm_mon+1,
22. last_modify_time->tm_mday,
23. last_modify_time->tm_hour,
24. last_modify_time->tm_min,
25. last_modify_time->tm_sec);
26.
27. return 0;
28. }
3.3获取文件类型
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <sys/stat.h>
4.
5.
6. int main()
7. {
8. struct stat mystat;
9. int ret = stat("linkfile", &mystat);
10. if(ret == -1)
11. {
12. perror("stat");
13. return -1;
14. }
15.
16. printf("size: %d\n", (int)mystat.st_size);
17.
18. //文件类型判断
19. if(S_ISREG(mystat.st_mode))
20. {
21. printf("普通文件...\n");
22. }
23. if(S_ISDIR(mystat.st_mode))
24. {
25. printf("目录...\n");
26. }
27. if(S_ISLNK(mystat.st_mode))
28. {
29. printf("软链接文件...\n");
30. }
31. return 0;
32. }
3.4获取文件权限
关于变量 st_mode:
- st_mode -- 16位整数
○ 0-2 bit -- 其他人权限
- S_IROTH 00004 读权限 100
- S_IWOTH 00002 写权限 010
- S_IXOTH 00001 执行权限 001
- S_IRWXO 00007 掩码, 过滤 st_mode中除其他人权限以外的信息
○ 3-5 bit -- 所属组权限
- S_IRGRP 00040 读权限
- S_IWGRP 00020 写权限
- S_IXGRP 00010 执行权限
- S_IRWXG 00070 掩码, 过滤 st_mode中除所属组权限以外的信息
○ 6-8 bit -- 文件所有者权限
- S_IRUSR 00400 读权限
- S_IWUSR 00200 写权限
- S_IXUSR 00100 执行权限
- S_IRWXU 00700 掩码, 过滤 st_mode中除文件所有者权限以外的信息
○ 12-15 bit -- 文件类型
- S_IFSOCK 0140000 套接字
- S_IFLNK 0120000 符号链接(软链接)
- S_IFREG 0100000 普通文件
- S_IFBLK 0060000 块设备
- S_IFDIR 0040000 目录
- S_IFCHR 0020000 字符设备
- S_IFIFO 0010000 管道
- S_IFMT 0170000 掩码,过滤 st_mode中除文件类型以外的信息
############### 按位与操作举例 ###############
1111 1111 1111 1011 # st_mode
0000 0000 0000 0100 # S_IROTH
&
----------------------------------------
0000 0000 0000 0000 # 没有任何权限