linux文件之touch命令及文件时间戳


一,在将touch命名前先看看文件关于时间的属性。通过stat命令查看文件如下:
 
[root@localhost test]# stat f1
 File: `f1'
 Size: 34              Blocks:8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 72757       Links: 1
Access: (0640/-rw-r-----)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-10-09 01:26:41.658883440 +0800
Modify: 2015-10-08 04:05:15.000000000 +0800
Change: 2015-10-08 20:20:32.811903158 +0800
 
查看上面信息看到文件三个属性
 
Acess time:即文件存取时间,或者理解为“最后一次读取时间”,如使用touch、cat、more等命令会修改此数值,但使用ls,stat查看不会改变。注意:若你使用的是虚拟机测试,会发现cat、more等命令时不会实时更新,可以使用实体机测试。
 
Modify time:修改时间,这里是指文件内容最后一次修改时间。ls命令默认显示就是这个时间。
 
Change time:改变时间,这里是指文件属性最后修改时间,如修改权限、名称等,一定要与Modify time区分。
 
二、touch命令:用来修改文件时间戳,或者新建文件。
 
1,选项参数
-a:--time=atime   Acess time;
-m: --time=mtime   Modify time;
 
-c:     --no-create不建立任何文档。默认当文件不存在时会创建文件,-c就可以不创建文件。
 
-r:将文件设置和参考文件的日期时间相同。
 
-t:使用指定时间日期,而不是直接用系统时间。
 
三、实例
 
1,先来touch一个已存在的文件
 
[root@localhost test]# stat f1
 File: `f1'
 Size: 34              Blocks:8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 71660       Links: 1
Access: (0640/-rw-r-----)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-10-09 17:28:43.853828458 +0800
Modify: 2015-10-08 04:05:15.000000000 +0800
Change: 2015-10-09 03:16:57.117877086 +0800
[root@localhost test]# touch f1
[root@localhost test]# stat f1 #可以看到所有文件时间都更新为当前系统时间。
 File: `f1'
 Size: 34              Blocks:8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 71660       Links: 1
Access: (0640/-rw-r-----)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-10-09 18:17:22.936820504 +0800
Modify: 2015-10-09 18:17:22.936820504 +0800
Change: 2015-10-09 18:17:22.936820504 +0800
[root@localhost test]# date
Fri Oct 9 18:17:29 CST 2015
 
2,通过touch命令创建一个文件
 
[root@localhost test]# touch -c f3 #-c不创建文件
[root@localhost test]# ls
f1 f2  test
[root@localhost test]# touch f3 #创建文件f3
[root@localhost test]# ls
f1 f2  f3  test
[root@localhost test]# file f3 #查看f3文件类型,显示为空
f3: empty
 
3,修改文件时间为指定时间
 
通过man查看touch –t参数:
-t STAMP  use [[CC]YY]MMDDhhmm[.ss] instead of currenttime
 
[root@localhost test]# touch -t201510081010.10 f3
[root@localhost test]# stat f3 #修改时间为指定时间日期
 File: `f3'
 Size: 4               Blocks:8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 66037       Links: 1
Access: (0644/-rw-r--r--)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-10-08 10:10:10.000000000 +0800
Modify: 2015-10-08 10:10:10.000000000 +0800
 
Change: 2015-10-09 18:34:22.535825523 +0800#注意Ctime为当前时间,因为更改文件属性。
 
4,将文件时间与指定参考文件时间对齐。
 
[root@localhost test]# touch -r f3 f1 #将f1时间改为与f3相同
[root@localhost test]# stat f1
 File: `f1'
 Size: 34              Blocks:8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 71660       Links: 1
Access: (0640/-rw-r-----)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-10-08 10:10:10.000000000 +0800
Modify: 2015-10-08 10:10:10.000000000 +0800
Change: 2015-10-09 18:42:47.175819324 +0800#同样Ctime依然当前时间。
 
总结:touch命令还是比较简单的,主要是理解关于文件的三个时间属性。

相关内容