目录
一、理论
1.stat命令
二、实验
1.获取命令帮助
2.查看命令版本
3.查看文件状态信息
4.-f查看文件所在文件系统状态信息
5.-t以简洁形式输出文件信息
6.-L显示软连接原始文件状态信息
7.获取文件inode
8.获取文件所属用户或者用户ID
9.获取文件所属用户组或者用户组ID
10.获取文件磁盘挂载点
11.获取文件访问时间
12.获取文件修改时间
13.获取文件更改时间
14.获取文件权限
15.时间更新演示
一、理论
1.stat命令
(1)概念
stat命令用于显示文件的状态信息。stat命令的输出信息比ls命令的输出信息要更详细。stat命令可以查看的信息包括:
表1 stst命令查看信息
| 查看信息 | 描述 |
| File | 显示文件名 |
| Size | 显示文件大小 |
| Blocks | 文件使用的数据块总数 |
| IO Block | IO块大小 |
| regular file | 文件类型(常规文件) |
| Device | 设备编号 |
| Inode | Inode号 |
| Links | 链接数 |
| Access | 文件的权限 |
| Gid、Uid | 文件所有权的Gid和Uid |
| access time | 最后存取时间(ls -lu filename),表示最后一次访问(仅仅是访问,没有改动)文件的时间 |
| modify time | 最后修改时间(ls -l filename ),表示最后一次修改文件的时间 |
| change time | 最后更改时间(ls -lc filename ),表示最后一次对文件属性改变的时间,包括权限,大小,属性等等 |
| Birth time | 文件创建时间,crtime,不过据查此属性linux已废弃,目前状态显示结果均为- |
(2)语法
用法:stat [OPTION]… FILE…
(3)常用参数选项
表2 stat命令常用选项
| 参数选项 | 功能 |
| -f |
查看文件所在文件系统状态信息 |
| -t |
以简洁形式输出文件信息 |
| -L |
显示软连接原始文件状态信息 |
| -c | 按照指定格式输出 |
(4)文件的有效格式序列
表3 文件有效格式序列
| 格式符 | 格式符说明 |
| %a | 八进制中的访问权限(777显示) |
| %A | 人类可读形式的访问权(rwx显示) |
| %b | 分配的块数(请参阅%B) |
| %B | %b报告的每个块的大小(字节) |
| %C | SELinux安全上下文字符串 |
| %d | 十进制设备编号 |
| %D | 十六进制的设备编号 |
| %f | 十六进制原始模式 |
| %F | 文件类型 |
| %g | 所有者的组ID |
| %G | 所有者的组名称 |
| %h | 硬链接数 |
| %i | inode数 |
| %m | 挂载点 |
| %n | 文件名 |
| %N | 带取消引用(如果是符号链接)的带引号的文件名 |
| %o | **I/O传输大小提示 |
| %s | 总大小,以字节为单位 |
| %t | 主要设备类型(十六进制),用于字符/块设备特殊文件 |
| %T | 次要设备类型(十六进制),用于字符/块设备特殊文件 |
| %u | 所有者的用户ID |
| %U | 所有者的用户名 |
| %w | 文件出生时间,人类可读;-如果未知 |
| %W | 文件生成时间,自纪元起的秒数;如果未知,则为0 |
| %x | 最后一次访问的时间,人类可读 |
| X | 上次访问的时间,自纪元起的秒数 |
| %y | 上次修改的时间,人类可读 |
| %Y | 上次修改的时间,自纪元起的秒数 |
| %z | 最后更改的时间,人类可读 |
| %Z | 上次更改的时间,自新纪元起的秒数 |
(5)文件系统的有效格式序列
表4 文件系统有效格式序列
| 格式符 | 格式符说明 |
| %a | 非超级用户可用的空闲块 |
| %b | 文件系统中的数据块总数 |
| %c | 文件系统中的文件节点总数 |
| %d | 文件系统中的空闲文件节点 |
| %f | 文件系统中的空闲块 |
| %i | 十六进制文件系统ID |
| %l | 文件名的最大长度 |
| %n | 文件名 |
| %s | 块大小(用于更快的传输) |
| %S | 基本块大小(用于块计数) |
| %t | 十六进制文件系统类型 |
| %T | 人类可读形式的文件系统类型 |
二、实验
1.获取命令帮助
[root@centos2 /]# stat --help 用法:stat [选项]... 文件... Display file or file system status. Mandatory arguments to long options are mandatory for short options too. -L, --dereference follow links -f, --file-system display file system status instead of file status -c --format=FORMAT use the specified FORMAT instead of the default; output a newline after each use of FORMAT --printf=FORMAT like --format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT -t, --terse print the information in terse form --help 显示此帮助信息并退出 --version 显示版本信息并退出 The valid format sequences for files (without --file-system): %a access rights in octal %A access rights in human readable form %b number of blocks allocated (see %B) %B the size in bytes of each block reported by %b %C SELinux security context string %d device number in decimal %D device number in hex %f raw mode in hex %F file type %g group ID of owner %G group name of owner %h number of hard links %i inode number %m mount point %n file name %N quoted file name with dereference if symbolic link %o optimal I/O transfer size hint %s total size, in bytes %t major device type in hex, for character/block device special files %T minor device type in hex, for character/block device special files %u user ID of owner %U user name of owner %w time of file birth, human-readable; - if unknown %W time of file birth, seconds since Epoch; 0 if unknown %x time of last access, human-readable %X time of last access, seconds since Epoch %y time of last modification, human-readable %Y time of last modification, seconds since Epoch %z time of last change, human-readable %Z time of last change, seconds since Epoch Valid format sequences for file systems: %a free blocks available to non-superuser %b total data blocks in file system %c total file nodes in file system %d free file nodes in file system %f free blocks in file system %i file system ID in hex %l maximum length of filenames %n file name %s block size (for faster transfers) %S fundamental block size (for block counts) %t file system type in hex %T file system type in human readable form
讯享网
2.查看命令版本
讯享网[root@centos2 /]# stat --version stat (GNU coreutils) 8.22
3.查看文件状态信息
[root@centos2 /]# stat disk.sh 文件:"disk.sh" 大小:358 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode: 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 环境:unconfined_u:object_r:etc_runtime_t:s0 最近访问:2023-06-07 19:40:02. +0800 最近更改:2023-06-07 19:40:02. +0800 最近改动:2023-06-07 19:40:02. +0800 创建时间:-
4.-f查看文件所在文件系统状态信息
讯享网[root@centos2 /]# stat -f disk.sh 文件:"disk.sh" ID:fd0000000000 文件名长度:255 类型:xfs 块大小:4096 基本块大小:4096 块:总计: 空闲: 可用: Inodes: 总计: 空闲:
5.-t以简洁形式输出文件信息
简洁形式主要是把每个值的列头去掉了,只显示值,三个时间按照纪元至今秒数显示。
[root@centos2 /]# stat -t disk.sh disk.sh 358 8 81a4 0 0 fd00 90130 1 0 0 0 4096 unconfined_u:object_r:etc_runtime_t:s0
6.-L显示软连接原始文件状态信息
创建软连接
讯享网[root@centos2 /]# ln -s /disk.sh /abc
[root@centos2 /]# stat abc 文件:"abc" -> "/disk.sh" 大小:8 块:0 IO 块:4096 符号链接 设备:fd00h/64768d Inode:90322 硬链接:1 权限:(0777/lrwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root) 环境:unconfined_u:object_r:root_t:s0 最近访问:2023-06-12 15:59:29. +0800 最近更改:2023-06-12 15:59:29. +0800 最近改动:2023-06-12 15:59:29. +0800 创建时间:- [root@centos2 /]# stat -L abc 文件:"abc" 大小:358 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode:90130 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 环境:unconfined_u:object_r:etc_runtime_t:s0 最近访问:2023-06-12 15:48:08. +0800 最近更改:2023-06-12 15:48:08. +0800 最近改动:2023-06-12 15:48:08. +0800 创建时间:-
7.获取文件inode
讯享网[root@centos2 /]# stat -c %i disk.sh 90130
8.获取文件所属用户或者用户ID
[root@centos2 /]# stat -c %u disk.sh 0
讯享网[root@centos2 /]# stat -c %U disk.sh root
9.获取文件所属用户组或者用户组ID
[root@centos2 /]# stat -c %G disk.sh root
讯享网[root@centos2 /]# stat -c %g disk.sh 0
10.获取文件磁盘挂载点
[root@centos2 /]# stat -c %m disk.sh /
11.获取文件访问时间
讯享网[root@centos2 /]# stat -c %x disk.sh 2023-06-12 15:48:08. +0800
12.获取文件修改时间
[root@centos2 /]# stat -c %y disk.sh 2023-06-12 15:48:08. +0800
13.获取文件更改时间
讯享网[root@centos2 /]# stat -c %z disk.sh 2023-06-12 16:03:15. +0800
14.获取文件权限
[root@centos2 /]# stat -c %A disk.sh -rw-r--r--
讯享网[root@centos2 /]# stat -c %a disk.sh 644
15.时间更新演示
(1)使用vim命令打开文件,即使不做任何修改,也会更新mtime
[root@centos2 /]# stat -c %y disk.sh 2023-06-12 16:26:39. +0800
(2)修改文件权限或属主,ctime更新,atime和mtime不会更新
讯享网[root@centos2 /]# chown david:david disk.sh
[root@centos2 /]# stat disk.sh 文件:"disk.sh" 大小:358 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode: 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 1005/ david) Gid:( 1005/ david) 环境:unconfined_u:object_r:etc_runtime_t:s0 最近访问:2023-06-12 16:26:39. +0800 最近更改:2023-06-12 16:26:39. +0800 最近改动:2023-06-12 16:28:17. +0800 创建时间:-
(3)查看文件,atime更新,mtime和ctime不会更新
讯享网[root@centos2 /]# cat disk.sh 1 time=$(date "+%Y-%m-%d %H:%M:%S") 2 diskuse=`df -h | sed -n '2p' | awk '{print $5}'` 3 disk=`echo "$diskuse" | cut -d "%" -f 1` 4 echo "当前硬盘使用率为:$disk%" 5 if [ $disk -gt 15 ] 6 then 7 echo "$time 您当前磁盘使用率$disk%,已严重超标" | mail -s "email" 8 else 9 exit 10 fi
[root@centos2 /]# stat disk.sh 文件:"disk.sh" 大小:358 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode: 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 1005/ david) Gid:( 1005/ david) 环境:unconfined_u:object_r:etc_runtime_t:s0 最近访问:2023-06-12 16:31:49. +0800 最近更改:2023-06-12 16:26:39. +0800 最近改动:2023-06-12 16:28:17. +0800 创建时间:-

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/42943.html