[LINUX]警告:檢測到時鍾錯誤。您的創建可能是不完整的。
============================
linux批量修改文件時間的兩種方法
atime、mtime、ctime
Linux系統文件有三個主要的時間屬性,分別是ctime(change time, 而不是create time), atime(access time), mtime(modify time)。后來為了解決atime的性能問題,還引入了一個relatime的屬性,下面一一解釋。
ctime, 很多朋友把它理解成create time,包括很多誤導人的書籍也是這么寫。實際上ctime是指change time。
mtime, 就是modify time。
mtime和ctime的區別在於,只有修改了文件的內容,才會更新文件的mtime,而對文件更名,修改文件的屬主等操作,只會更新ctime。
舉例說明: 對文件進行mv操作,mtime不變,ctime更新;編輯文件內容,mtime和ctime同時修改。其他操作的影響,可以自己試驗。但是我發現對文件執行touch操作,會同時修改mtime和ctime,所以具體修改哪個時間,還取決於不同命令自己的實現;
atime, 這個就是每次查看文件內容的時候會更新。比如cat操作,而ls操作是不會更新的。
你可以通過stat來查看文件的mtime,ctime,atime等屬性,也可以通過ls命令來查看,具體如下:
ls -lc filename 列出文件的 ctime
ls -lu filename 列出文件的 atime
ls -l filename 列出文件的 mtime
從kernel2.6.29開,還默認集成了一個relatime的屬性。這個功能我個人理解是為了解決atime的兼容性問題。因為在文件讀操作很頻繁的系統中,atime更新所帶來的開銷很大,所以很多SA都在掛裝文件系統的時候使用noatime屬性來停止更新atime。但是有些程序需要根據atime進行一些判斷和操作,所以Linux就推出了一個relatime特性。
使用這個特性來掛裝文件系統后,只有當mtime比atime更新的時候,才會更新atime。事實上,這個時候atime和mtime已經是同一個東西了。所以這個選項就是為了實現對atime的兼容才推出的。並不是一個新的時間屬性。使用方法就是通過mount -o relatime /dir來掛裝目錄。
在網上找到了一個表,列出了常見的系統調用對這幾個時間的影響,包括對父目錄的影響,很清晰。
On Thu, Oct 05, 2006 at 12:33:57PM -0700, Dmitri V. Ivanov wrote:
> *********************************************************************
> Due to deletion of content types excluded from this list by policy,
> this multipart message was reduced to a single part, and from there
> to a plain text message.
> *********************************************************************
It seems I've failed to send html version...
There is a my opinion about 'Why' and 'How' and next to it my script.
It seems to me too slow. It sorts all files with directory whereas to
sort directories is sufficient. But to sort all files seems to be
usefull to get list of deleted files like uniq.
Why and How:
=========
First of all about file timestamps and their update [1]in common and table
with details:
+-------------------------------------------------+
| | timestamps marked for update |
| syscall |---------------------------------|
| | file | parent dir |
|---------------+-------------------+-------------|
| [2]chdir | | |
|---------------| - | - |
| [3]fchdir | | |
|---------------+-------------------+-------------|
| [4]chmod | | |
|---------------| ctime | - |
| [5]fchmod | | |
|---------------+-------------------+-------------|
| [6]chown | | |
|---------------| | |
| [7]fchown | ctime | - |
|---------------| | |
| [8]lchown | | |
|---------------+-------------------+-------------|
| [9]close | - | - |
|---------------+-------------------+-------------|
| [10]creat | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [11]execve | atime | - |
|---------------+-------------------+-------------|
| [12]fcntl | - | - |
|---------------+-------------------+-------------|
| [13]ftruncate | | |
|---------------| ctime,mtime | - |
| [14]truncate | | |
|---------------+-------------------+-------------|
| [15]fstat | | |
|---------------| | |
| [16]stat | - | - |
|---------------| | |
| [17]lstat | | |
|---------------+-------------------+-------------|
| [18]fsync | | |
|---------------| - | - |
| [19]fdatasync | | |
|---------------+-------------------+-------------|
| [20]link | ctime | ctime,mtime |
|---------------+-------------------+-------------|
| [21]lseek | - | - |
|---------------+-------------------+-------------|
| [22]mknod | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [23]mkdir | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [24]mmap | * | - |
|---------------+-------------------+-------------|
| [25]munmap | - | - |
|---------------+-------------------+-------------|
| [26]msync | * | - |
|---------------+-------------------+-------------|
| [27]open | * | * |
|---------------+-------------------+-------------|
| [28]pread | | |
|---------------| | |
| [29]read | atime | - |
|---------------| | |
| [30]readv | | |
|---------------+-------------------+-------------|
| [31]pwrite | | |
|---------------| | |
| [32]write | ctime,mtime | - |
|---------------| | |
| [33]writev | | |
|---------------+-------------------+-------------|
| [34]rename | implementation | ctime,mtime |
|---------------+-------------------+-------------|
| [35]rmdir | - | ctime,mtime |
|---------------+-------------------+-------------|
| [36]readlink | * | - |
|---------------+-------------------+-------------|
| [37]readdir | atime | - |
|---------------+-------------------+-------------|
| readahead | ? | ? |
|---------------+-------------------+-------------|
| [38]symlink | * | * |
|---------------+-------------------+-------------|
| sendfile | ? | ? |
|---------------+-------------------+-------------|
| [39]unlink | - | ctime,mtime |
|---------------+-------------------+-------------|
| [40]utime | ctime | - |
|---------------+-------------------+-------------|
一、查看文件時間及相關命令
1、stat查看文件時間
[root@web10 ~]# stat install.logFile: “install.log”Size: 33386 Blocks: 80 IO Block: 4096 一般文件Device: fd00h/64768d Inode: 7692962 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2012-07-13 16:02:34.000000000 +0800Modify: 2011-11-29 16:03:06.000000000 +0800Change: 2011-11-29 16:03:08.000000000 +0800
說明:Access訪問時間。Modify修改時間。Change狀態改變時間。可以stat *查看這個目錄所有文件的狀態。
而我們想要查看某文件的三個時間中的具體某個時間,並以年月日時分秒的格式保存。我們可以使用下面的命令:
[root@web10 ~]# stat install.log|grep -i Modify | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}'20111129160306
2、ls查看文件時間
相應的通過ls 查看時也有三個時間:
• modification time(mtime,修改時間):當該文件的“內容數據”更改時,就會更新這個時間。內容數據指的是文件的內容,而不是文件的屬性。
• status time(ctime,狀態時間):當該文件的”狀態(status)”改變時,就會更新這個時間,舉例來說,更改了權限與屬性,就會更新這個時間。
• access time(atime,存取時間):當“取用文件內容”時,就會更新這個讀取時間。舉例來說,使用cat去讀取 ~/.bashrc,就會更新atime了。
[root@web10 ~]# ls -l --time=ctime install.log-rw-r--r-- 1 root root 33386 2011-11-29 install.log [root@web10 ~]# ls -l --time=atime install.log-rw-r--r-- 1 root root 33386 07-13 16:02 install.log
注意:ls參數里沒有--mtime這個參數,因為我們默認通過ls -l查看到的時間就是mtime 。
二、修改文件時間
創建文件我們可以通過touch來創建。同樣,我們也可以使用touch來修改文件時間。touch的相關參數如下:
-a : 僅修改access time。-c : 僅修改時間,而不建立文件。
-d : 后面可以接日期,也可以使用 --date="日期或時間"
-m : 僅修改mtime。
-t : 后面可以接時間,格式為 [YYMMDDhhmm]
注:如果touch后面接一個已經存在的文件,則該文件的3個時間(atime/ctime/mtime)都會更新為當前時間。若該文件不存在,則會主動建立一個新的空文件。
[root@web10 ~]# touch install.log[root@web10 ~]# stat install.logFile: “install.log”Size: 33386 Blocks: 80 IO Block: 4096 一般文件Device: fd00h/64768d Inode: 7692962 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2012-07-13 16:21:50.000000000 +0800Modify: 2012-07-13 16:21:50.000000000 +0800Change: 2012-07-13 16:21:50.000000000 +0800
同樣,使用ls ,查看到的結果也一樣。
[root@web10 ~]# ls -l --time=ctime install.log-rw-r--r-- 1 root root 33386 07-13 16:21 install.log [root@web10 ~]# ls -l --time=atime install.log-rw-r--r-- 1 root root 33386 07-13 16:21 install.log [root@web10 ~]# ls -l install.log-rw-r--r-- 1 root root 33386 07-13 16:21 install.log
下面再看一個和touch不相關的例子:
[root@web10 ~]# cp /etc/profile .;ll --time=atime profile ;ll --time=ctime profile cp:是否覆蓋“./profile”? y -rw-r--r-- 1 root root 1344 07-13 16:24 profile -rw-r--r-- 1 root root 1344 07-13 16:25 profile
因為我之前運行過這個命令一次,所以會出現覆蓋,不過這個覆蓋出的好,剛才讓我們看到了atime和ctime的時間的差別。
我們再回到touch利用touch修改文件時間:
1. 同時修改文件的修改時間和訪問時間 touch -d "2010-05-31 08:10:30" install.log 2. 只修改文件的修改時間 touch -m -d "2010-05-31 08:10:30" install.log 3. 只修改文件的訪問時間 touch -a -d "2010-05-31 08:10:30" install.log
下面再給一個rootkit木馬常用的伎倆。就是把后一個文件的時間修改成和前一個相同。
touch -acmr /bin/ls /etc/sh.conf
另外touch還支持像date命令一樣參數修改文件時間:
[root@web10 ~]# touch -d "2 days ago" install.log ; ll install.log-rw-r--r-- 1 root root 33386 07-11 16:35 install.log
最后總結下常用的文件操作與時間的關系:
1、訪問時間,讀一次這個文件的內容,這個時間就會更新。比如對這個文件使用more命令。ls、stat命令都不會修改文件的訪問時間。
2、修改時間,對文件內容修改一次,這個時間就會更新。比如:vim后保存文件。ls -l列出的時間就是這個時間。
3、狀態改變時間。通過chmod命令更改一次文件屬性,這個時間就會更新。查看文件的詳細的狀態、准確的修改時間等,可以通過stat命令 文件名。
==========================================