Linux命令:stat

目录

一、理论

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 BlockIO块大小
regular file文件类型(常规文件)
Device设备编号
InodeInode号
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报告的每个块的大小(字节)
%CSELinux安全上下文字符串
%d十进制设备编号
%D十六进制的设备编号
%f十六进制原始模式
%F文件类型
%g所有者的组ID
%G所有者的组名称
%h硬链接数
%iinode数
%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:1944081     硬链接: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.680115173 +0800
最近更改:2023-06-07 19:40:02.680115173 +0800
最近改动:2023-06-07 19:40:02.682115156 +0800
创建时间:-

4.-f查看文件所在文件系统状态信息

[root@centos2 /]# stat -f disk.sh
  文件:"disk.sh"
    ID:fd0000000000 文件名长度:255     类型:xfs
块大小:4096       基本块大小:4096
    块:总计:13100800   空闲:10966314   可用:10966314
Inodes: 总计:26214400   空闲:25928770

5.-t以简洁形式输出文件信息

简洁形式主要是把每个值的列头去掉了,只显示值,三个时间按照纪元至今秒数显示。

[root@centos2 /]# stat -t disk.sh
disk.sh 358 8 81a4 0 0 fd00 90130 1 0 0 1686556088 1686556088 1686556088 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.879388388 +0800
最近更改:2023-06-12 15:59:29.879388388 +0800
最近改动:2023-06-12 15:59:29.879388388 +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.482270985 +0800
最近更改:2023-06-12 15:48:08.482270985 +0800
最近改动:2023-06-12 15:48:08.507270807 +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.482270985 +0800

12.获取文件修改时间

[root@centos2 /]# stat -c %y disk.sh
2023-06-12 15:48:08.482270985 +0800

13.获取文件更改时间

[root@centos2 /]# stat -c %z disk.sh
2023-06-12 16:03:15.402715086 +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.579676612 +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:409342      硬链接: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.579676612 +0800
最近更改:2023-06-12 16:26:39.579676612 +0800
最近改动:2023-06-12 16:28:17.400003431 +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" 7jjw@163.com
  8 else
  9     exit
 10 fi
[root@centos2 /]# stat disk.sh 
  文件:"disk.sh"
  大小:358       	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:409342      硬链接: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.265545417 +0800
最近更改:2023-06-12 16:26:39.579676612 +0800
最近改动:2023-06-12 16:28:17.400003431 +0800
创建时间:-

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值