路漫漫其修远兮
吾将上下而求索

linux时间戳查看:stat命令

1、关于时间戳

每一个文件都有三种时间(称为时间戳timestamps),对这三种时间,很多时候容易混淆不清,因此这里要说明下:  

        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.  

Note:

如果修改文件(使用编辑器存盘或使用) >>转向操作),则Modify time和Change time会同步更新成写入的时间,但Access time不变  

如果执行touch文件,则3中时间全部改变

使用ln –s做文件的软式连接,会改变文件的取用时间

使用ls –la查看一般文件,不会更改这三种时间,但如果这个文件时符号链接文件,则会改变取用的时间(Access time)

2、文件有两个属性

元数据:metadata,文件的属性

数据:data,实际的文件内容

3、stat命令介绍

stat:display file or file system status 查看文本文件的属性,大小,块,权限,最近访问

centos7为了减少文件频繁修改,提高性能,访问时间不是实时更新的

在实际的系统中,访问时间作用不大,每次对文件进行读取操作都会更新文件属性,会增加磁盘的读写io压力,最好在挂载磁盘的时候,将磁盘的atime关闭,这样可以提高磁盘速度

4、常用选项介绍

-f, –file-system   display file system status instead of file status,查看指定磁盘的属性,块使用情况,inode使用情况

[root@localhost /tmp]#stat -f /dev/sda1
  File: "/dev/sda1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 80722      Free: 80675      Available: 80675
Inodes: Total: 80722      Free: 80084

-c  –format=FORMAT  use  the  specified  FORMAT  instead of the default; output a newline,就是只显示stat里面的某个属性

%x: 取用时间

%y: 修改时间

%z: 属性改动时间

[root@localhost /tmp]#stat test 
  File: `test'
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3024837     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/    zhao)   Gid: (    0/    root)
Access: 2017-05-07 12:21:13.219014776 +0800
Modify: 2017-05-07 12:27:31.906014565 +0800
Change: 2017-05-07 12:27:31.906014565 +0800

[root@localhost /tmp]#stat -c %x test 
2017-05-07 12:21:13.219014776 +0800

[root@localhost /tmp]#stat -c %y test 
2017-05-07 12:27:31.906014565 +0800

[root@localhost /tmp]#stat -c %z test 
2017-05-07 12:27:31.906014565 +0800

5、文件更改测试

[root@localhost /tmp]#echo abc > test
[root@localhost /tmp]#stat test #三个时间一致
  File: `test'
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3024837     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-07 12:21:02.321014882 +0800
Modify: 2017-05-07 12:21:02.321014882 +0800
Change: 2017-05-07 12:21:02.321014882 +0800

[root@localhost /tmp]#cat test #改变modify时间
abc
[root@localhost /tmp]#stat test 
  File: `test'
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3024837     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-07 12:21:13.219014776 +0800
Modify: 2017-05-07 12:21:02.321014882 +0800
Change: 2017-05-07 12:21:02.321014882 +0800

[root@localhost /tmp]#id zhao
uid=500(zhao) gid=500(zhao) groups=500(zhao)
[root@localhost /tmp]#chown zhao test #改变change时间,其他两个不改变
[root@localhost /tmp]#stat test 
  File: `test'
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3024837     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/    zhao)   Gid: (    0/    root)
Access: 2017-05-07 12:21:13.219014776 +0800
Modify: 2017-05-07 12:21:02.321014882 +0800
Change: 2017-05-07 12:22:03.346014845 +0800

[root@localhost /tmp]#ll test #是modify时间
-rw-r--r-- 1 zhao root 4 May  7 12:21 test

[root@localhost /tmp]#echo cde >> test #修改modify时间,change时间
[root@localhost /tmp]#stat test 
  File: `test'
  Size: 8         	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3024837     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/    zhao)   Gid: (    0/    root)
Access: 2017-05-07 12:21:13.219014776 +0800
Modify: 2017-05-07 12:22:49.354015394 +0800
Change: 2017-05-07 12:22:49.354015394 +0800
[root@localhost /tmp]#ll test 
-rw-r--r-- 1 zhao root 8 May  7 12:22 test

6、脚本中常用到

常见第一种,因为知识不全,不知道有第二种方式,,,所经历的坑都是由无知造成的

[root@localhost /tmp]#stat test | tail -2 | awk  '{print $2$3}' | cut -c1-10
2017-05-07
2017-05-07
[root@localhost /tmp]#stat -c %y test | cut -c1-10
2017-05-07

未经允许不得转载:江哥架构师笔记 » linux时间戳查看:stat命令

分享到:更多 ()

评论 抢沙发

评论前必须登录!