歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux基礎知識之文件的時間戳及touch的使用

Linux基礎知識之文件的時間戳及touch的使用

日期:2017/3/6 9:25:48   编辑:學習Linux

Linux基礎知識之文件的時間戳及touch的使用


Linux基礎知識之文件的時間戳及touch的使用


文件的時間戳包含在它的元數據中,屬於其本身屬性信息。
文件的時間戳包含有三種時間分別如下:
acess time 訪問時間
modify time 修改時間(更具體說是修改數據時的時間)
change time 改變時間 (修改元數據的時間)

modify time以下簡寫為mtime,mtime與ctime是不同的,當文件的屬性信息發生改變比如文件名,文件路徑,文件屬主等其改變的是ctime;當文件的內容發生改動則是mtime發生變化。

科普:
元數據的概念:
元數據(Metadata),又稱中介數據、中繼數據,為描述數據的數據(data about data),主要是描述數據屬性(property)的信息,用來支持如指示存儲位置、歷史數據、資源查找、文件記錄等功能。元數據算是一種電子式目錄,為了達到編制目錄的目的,必須在描述並收藏數據的內容或特色,進而達成協助數據檢索的目的。都柏林核心集(Dublin Core Metadata Initiative,DCMI)是元數據的一種應用,是1995年2月由國際圖書館電腦中心(OCLC)和美國國家超級計算應用中心(National Center for Supercomputing Applications,NCSA)所聯合贊助的研討會,在邀請52位來自圖書館員、電腦專家,共同制定規格,創建一套描述網絡上電子文件之特征。元數據是關於數據的組織、數據域及其關系的信息,簡言之,元數據就是關於數據的數據。

了解文件時間戳的概念對於發生故障迅速定位問題所在有一定幫助。
如何查看文件的時間戳
命令:stat 它是查看文件系統狀態
修改文件的時間戳
命令:touch
為了對touch有個更詳細的了解我們man下touch其主要用法如下(有省略,只列舉常用的功能項)
NAME
touch - change file timestamps
SYNOPSIS
touch [OPTION]... FILE...
DESCRIPTION
Update the access and modification times of each FILE to the current
time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options
too.
-a change only the access time 修改atime
-d, --date=STRING
parse STRING and use it instead of current time
-h, --no-dereference 只修改鏈接文件時間戳而對鏈接的源文件無影響
affect each symbolic link instead of any referenced file (useful
only on systems that can change the timestamps of a symlink)
-m change only the modification time 修改mtime
-r, --reference=FILE 將此文件的時間戳與指定文件時間戳一致
use this file’s times instead of current time
-t STAMP 修改時間戳
use [[CC]YY]MMDDhhmm[.ss] instead of current time

當然touch還有一個很主要的功能就是創建新文件,其格式為:
touch filename 如果該file不存在則創建。

如何修改文件時間戳,通過實驗來查看上面選項的實際作用
以下實驗環境均在CentOS6.8環境,
實驗前准備:
/test目錄 /test/file1文件
[root@centos6 test]# pwd
/test
[root@centos6 test]# ll
總用量 0
-rw-r--r--. 1 root root 0 7月 28 21:43 file1

前提條件准備完畢。
先查看下文件file1的文件屬性信息,特別是時間戳
[root@centos6 test]# stat file1
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 803h/2051d Inode: 266584 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-28 21:43:53.554651380 +0800
Modify: 2016-07-28 21:43:53.554651380 +0800
Change: 2016-07-28 21:43:53.554651380 +0800

先修改atime

123456789 [root@centos6 test]# touch -a -t 201009200930 file1
[root@centos6 test]# stat file1
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 803h/2051d Inode: 266584 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2010-09-20 09:30:00.000000000 +0800
Modify: 2016-07-28 21:43:53.554651380 +0800
Change: 2016-07-28 21:48:15.589652240 +0800

-a指定為atime -t指定要修改的具體時間 要修改atime,需要兩者合用。
由結果可以看到atime改變了,同時ctime也發生變化,因為修改文件file1的屬性信息故只要修改關於時間戳的信息ctime均發生改變,其發生變化的時間即修改時系統當下時間。
下面修改mtime
[root@centos6 test]# touch -m -t 201607180830 file1
[root@centos6 test]# stat file1
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 803h/2051d Inode: 266584 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2010-09-20 09:30:00.000000000 +0800
Modify: 2016-07-18 08:30:00.000000000 +0800
Change: 2016-07-28 21:51:21.598641893 +0800

mtime發生改變,ctime也發生改變。
下面我們使用命令cat查看下file1文件
123456789 [root@centos6 test]# cat file1
[root@centos6 test]# stat file1
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 803h/2051d Inode: 266584 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-28 21:53:00.418655120 +0800
Modify: 2016-07-18 08:30:00.000000000 +0800
Change: 2016-07-28 21:51:21.598641893 +0800

因為file1文件為空故什麼也沒顯示,不過我們發現atime發生了變化,其變化的時間為當前系統時間。
atime時間發生變化,是因為觸發了該文件的讀屬性。
下面我們在file1文件內添加寫內容
[root@centos6 test]# echo www >> file1
[root@centos6 test]# stat file1
File: "file1"
Size: 4 Blocks: 8 IO Block: 4096 普通文件
Device: 803h/2051d Inode: 266584 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-28 21:53:00.418655120 +0800
Modify: 2016-07-28 21:55:22.358651684 +0800
Change: 2016-07-28 21:55:22.358651684 +0800

由結果可知mtime、ctime均發生改變,因為文件數據被修改,數據內容及元數據都發生變化。

時間戳的實際作用
在實際生產環境中關於時間戳的問題不多,不過有時會因為系統異常導致atime時間比系統時間提前,也就是在系統看來atime是未來的時間,這種情況會導致該文件無法正常讀取。這個時候就需要手動刷新下該文件的atime
刷新atime命令
touche -a file

刷新mtime命令
touche -m file

刷新後的file時間自動更新為系統當下時間。

雖然時間戳的作用對於一般管理員來說無關緊要,不過黑客對其卻很重視,如何成功入侵系統,並且在功成身退後又不被人發現其入侵的痕跡,合理的修改時間戳還是很關鍵的。

本文永久更新鏈接地址:

http://xxxxxx/Linuxjc/1147428.html TechArticle

Copyright © Linux教程網 All Rights Reserved