天天看點

Linux基礎知識之檔案的時間戳及touch的使用

檔案時間戳概念介紹

    檔案的時間戳包含在它的中繼資料中,屬于其本身屬性資訊。

    檔案的時間戳包含有三種時間分别如下:

    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 &gt;&gt; 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,如需轉載請自行聯系原作者

繼續閱讀