文件时间戳概念介绍
文件的时间戳包含在它的元数据中,属于其本身属性信息。
文件的时间戳包含有三种时间分别如下:
acess time 访问时间
modify time 修改时间(更具体说是修改数据时的时间)
change time 改变时间 (修改元数据的时间)
modify time以下简写为mtime,mtime与ctime是不同的,当文件的属性信息发生改变比如文件名,文件路径,文件属主等其改变的是ctime;当文件的内容发生改动则是mtime发生变化。
科普:
元数据的概念:
与文件时间戳相关的命令
为什么要了解时间戳:了解文件时间戳的概念对于发生故障迅速定位问题所在有一定帮助。
如何查看文件的时间戳
命令:stat 它是查看文件系统状态
修改文件的时间戳
命令:touch
为了对touch有个更详细的了解我们man下touch其主要用法如下(有省略,只列举常用的功能项)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code>NAME</code>
<code> </code><code>touch</code> <code>- change </code><code>file</code> <code>timestamps</code>
<code>SYNOPSIS</code>
<code> </code><code>touch</code> <code>[OPTION]... FILE...</code>
<code>DESCRIPTION</code>
<code> </code><code>Update the access and modification </code><code>times</code> <code>of each FILE to the current</code>
<code> </code><code>time</code><code>.</code>
<code> </code><code>A FILE argument that does not exist is created empty, unless -c or -h</code>
<code> </code><code>is supplied.</code>
<code> </code><code>A FILE argument string of - is handled specially and causes </code><code>touch</code> <code>to</code>
<code> </code><code>change the </code><code>times</code> <code>of the </code><code>file</code> <code>associated with standard output.</code>
<code> </code><code>Mandatory arguments to long options are mandatory </code><code>for</code> <code>short options</code>
<code> </code><code>too.</code>
<code> </code><code>-a change only the access </code><code>time</code> <code>修改atime</code>
<code> </code><code>-d, --</code><code>date</code><code>=STRING </code>
<code> </code><code>parse STRING and use it instead of current </code><code>time</code>
<code> </code><code>-h, --no-dereference 只修改链接文件时间戳而对链接的源文件无影响</code>
<code> </code><code>affect each symbolic link instead of any referenced </code><code>file</code> <code>(useful</code>
<code> </code><code>only on systems that can change the timestamps of a </code><code>symlink</code><code>)</code>
<code> </code><code>-m change only the modification </code><code>time</code> <code>修改mtime</code>
<code> </code><code>-r, --reference=FILE 将此文件的时间戳与指定文件时间戳一致</code>
<code> </code><code>use this </code><code>file</code><code>’s </code><code>times</code> <code>instead of current </code><code>time</code>
<code> </code><code>-t STAMP 修改时间戳</code>
<code> </code><code>use [[CC]YY]MMDDhhmm[.ss] instead of current </code><code>time</code>
当然touch还有一个很主要的功能就是创建新文件,其格式为:
touch filename 如果该file不存在则创建。
如何修改文件的时间戳(附实验)
如何修改文件时间戳,通过实验来查看上面选项的实际作用
以下实验环境均在CentOS6.8环境,
实验前准备:
/test目录 /test/file1文件
<code>[root@centos6 </code><code>test</code><code>]</code><code># pwd</code>
<code>/test</code>
<code>[root@centos6 </code><code>test</code><code>]</code><code># ll</code>
<code>总用量 0</code>
<code>-rw-r--r--. 1 root root 0 7月 28 21:43 file1</code>
前提条件准备完毕。
先查看下文件file1的文件属性信息,特别是时间戳
<code>[root@centos6 </code><code>test</code><code>]</code><code># stat file1 </code>
<code> </code><code>File: </code><code>"file1"</code>
<code> </code><code>Size: 0 Blocks: 0 IO Block: 4096 普通空文件</code>
<code>Device: 803h</code><code>/2051d</code> <code>Inode: 266584 Links: 1</code>
<code>Access: (0644</code><code>/-rw-r--r--</code><code>) Uid: ( 0/ root) Gid: ( 0/ root)</code>
<code>Access: 2016-07-28 21:43:53.554651380 +0800</code>
<code>Modify: 2016-07-28 21:43:53.554651380 +0800</code>
<code>Change: 2016-07-28 21:43:53.554651380 +0800</code>
先修改atime
<code>[root@centos6 </code><code>test</code><code>]</code><code># touch -a -t 201009200930 file1</code>
<code>Access: 2010-09-20 09:30:00.000000000 +0800</code>
<code>Change: 2016-07-28 21:48:15.589652240 +0800</code>
-a指定为atime -t指定要修改的具体时间 要修改atime,需要两者合用。
由结果可以看到atime改变了,同时ctime也发生变化,因为修改文件file1的属性信息故只要修改关于时间戳的信息ctime均发生改变,其发生变化的时间即修改时系统当下时间。
下面修改mtime
<code>[root@centos6 </code><code>test</code><code>]</code><code># touch -m -t 201607180830 file1 </code>
<code>Modify: 2016-07-18 08:30:00.000000000 +0800</code>
<code>Change: 2016-07-28 21:51:21.598641893 +0800</code>
mtime发生改变,ctime也发生改变。
下面我们使用命令cat查看下file1文件
<code>[root@centos6 </code><code>test</code><code>]</code><code># cat file1 </code>
<code>Access: 2016-07-28 21:53:00.418655120 +0800</code>
因为file1文件为空故什么也没显示,不过我们发现atime发生了变化,其变化的时间为当前系统时间。
atime时间发生变化,是因为触发了该文件的读属性。
下面我们在file1文件内添加写内容
<code>[root@centos6 </code><code>test</code><code>]</code><code># echo www >> file1 </code>
<code> </code><code>Size: 4 Blocks: 8 IO Block: 4096 普通文件</code>
<code>Modify: 2016-07-28 21:55:22.358651684 +0800</code>
<code>Change: 2016-07-28 21:55:22.358651684 +0800</code>
由结果可知mtime、ctime均发生改变,因为文件数据被修改,数据内容及元数据都发生变化。
时间戳的实际作用
在实际生产环境中关于时间戳的问题不多,不过有时会因为系统异常导致atime时间比系统时间提前,也就是在系统看来atime是未来的时间,这种情况会导致该文件无法正常读取。这个时候就需要手动刷新下该文件的atime
刷新atime命令
<code>touche -a </code><code>file</code>
刷新mtime命令
<code>touche -m </code><code>file</code>
刷新后的file时间自动更新为系统当下时间。
虽然时间戳的作用对于一般管理员来说无关紧要,不过黑客对其却很重视,如何成功入侵系统,并且在功成身退后又不被人发现其入侵的痕迹,合理的修改时间戳还是很关键的。
本文转自 紫色的茶碗 51CTO博客,原文链接:http://blog.51cto.com/chawan/1831385,如需转载请自行联系原作者