一、Linux stat命令 语法
Linux stat命令 语法如下所示
stat [OPTION]... FILE...
主要参数
-L, --dereference
follow links
-Z, --context
print the SELinux security context
-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 display this help and exit
--version
output version information and exit
其中用的比较多的是 -f 参数和 -c参数。
-f 参数是显示文件系统信息 , -c是按照规定格式输出。
其中 -c 的可用参数主要有以下几个:
%A:用文件权限代码来表示权限
%a:用数字代码来表示权限
%F:用八进制表示文件权限
%G:文件拥有者的组名
%g:文件拥有者的属组id(gid)
%i:inode编号
%n:文件名
%s:文件大小
%U:文件拥有者名称
%u:文件拥有者的id(uid)
%x: 取用时间
%y: 修改时间
%z: 属性改动时间
注意以上的格式都需要使用-c来指定的 比如想看文件的修改时间,可以用
stat -c "%Y" file
示例:
第一行命令是得到文件的Modify时间在转换成时间格式,在和1970-01-01 00:00:00时间做差等到一个second时间
第二行是求出系统的时间和1970-01-01 00:00:00时间做差等到一个second时间
在2个直接相比较就可以。如果2个值相减等于60我就开始处理等等
zhouwj@zhouwj-virtual-machine:~$ date +%s -d "`stat -c '%y' aaa.txt`"
1563864320
zhouwj@zhouwj-virtual-machine:~$ date +%s
1563867929
zhouwj@zhouwj-virtual-machine:~$ date -d @1563867
Mon Jan 19 10:24:27 CST 1970
---------------------
接下来还是通过几个例子来深入学习下 stat 命令 。
二、Linux stat命令 使用示例
1)查看文件信息
[linuxdaxue.com@/home/linuxdaxue.com]$stat file_new
File: `file_new'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 655637 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 111/linuxdaxue.com) Gid: ( 503/linuxdaxue.com)
Access: 2016-12-31 22:34:50.000000000 +0800
Modify: 2016-12-31 22:34:50.000000000 +0800
Change: 2016-06-16 14:30:33.233275690 +0800
说明:
Access time(atime):是指取用文件的时间,所谓取用,常见的操作有:使用编辑器查看文件内容,使用cat命令显示文件内容,使用cp命令把该文件(即来源文件)复制成其他文件,或者在这个文件上运用grep sed more less tail head 等命令,凡是读取而不修改文件的操作,均衡改变文件的Access time.
Modify time(mtime):是指修改文件内容的时间,只要文件内容有改动(如使用转向输出或转向附加的方式)或存盘的操作,就会改变文件的Modify time,平常我们使用ls –l查看文件时,显示的时间就是Modify time
Change time(ctime):是指文件属性或文件位置改动的时间,如使用chmod,chown,mv指令集使用ln做文件的硬是连接,就会改变文件的Change time.
2)使用固定格式查看文件access time。
[linuxdaxue.com@/home/linuxdaxue.com]$stat -c "Access time is %x" file_new
Access time is 2016-12-31 22:34:50.000000000 +0800
3)查看文件系统信息
[linuxdaxue.com@/home/linuxdaxue.com]$stat -f /dev/
File: "/dev/"
ID: 0 Namelen: 255 Type: tmpfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 124777 Free: 124738 Available: 124738
Inodes: Total: 124777 Free: 124220
使用 -f 命令可以查看文件系统信息