linux下文件時(shí)間主要有下面三種:
1.1 modification time(mtime)
文件修改時(shí)間,即文件內(nèi)容的修改時(shí),更新這個(gè)時(shí)間,不包括文件權(quán)限和屬性的修改,使用ls -l查看,默認(rèn)顯示時(shí)間為mtime.
- $ ls -l uconv.h
- -rw-rw-r-- 1 work work 1808 Jul 23 2013 uconv.h
- 1.2 status time(ctime)
文件狀態(tài)status的修改時(shí)間,如文件的權(quán)限和屬性修改時(shí)更新這個(gè)時(shí)間,使用 ls --time=ctime 查看.
- $ ls -l --time=ctime uconv.h
- -rw-rw-r-- 1 work work 1808 Jul 23 2013 uconv.h
1、stat查看文件時(shí)間
- [root@web10 ~]# stat install.log
- File: “install.log”
- Size: 33386 Blocks: 80 IO Block: 4096 一般文件
- Device: fd00h/64768d Inode: 7692962 Links: 1
- Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
- Access: 2012-07-13 16:02:34.000000000 +0800
- Modify: 2011-11-29 16:03:06.000000000 +0800
- Change: 2011-11-29 16:03:08.000000000 +0800
說(shuō)明:Access訪(fǎng)問(wèn)時(shí)間,Modify修改時(shí)間,Change狀態(tài)改變時(shí)間,可以stat *查看這個(gè)目錄所有文件的狀態(tài),而我們想要查看某文件的三個(gè)時(shí)間中的具體某個(gè)時(shí)間,并以年月日時(shí)分秒的格式保存,我們可以使用下面的命令:
- [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 --Vevb.com
- 1.3 access time(atime)
文件訪(fǎng)問(wèn)時(shí)間,當(dāng)文件內(nèi)容被獲取時(shí),更新這個(gè)時(shí)間,使用 ls --time=actime 查看.
$ ls -l --time=atime uconv.h
-rw-rw-r-- 1 work work 1808 Dec 12 2013 uconv.h
相應(yīng)的通過(guò)ls 查看時(shí)也有三個(gè)時(shí)間:
• modification time(mtime,修改時(shí)間):當(dāng)該文件的“內(nèi)容數(shù)據(jù)”更改時(shí),就會(huì)更新這個(gè)時(shí)間。內(nèi)容數(shù)據(jù)指的是文件的內(nèi)容,而不是文件的屬性。
• status time(ctime,狀態(tài)時(shí)間):當(dāng)該文件的”狀態(tài)(status)”改變時(shí),就會(huì)更新這個(gè)時(shí)間,舉例來(lái)說(shuō),更改了權(quán)限與屬性,就會(huì)更新這個(gè)時(shí)間。
• access time(atime,存取時(shí)間):當(dāng)“取用文件內(nèi)容”時(shí),就會(huì)更新這個(gè)讀取時(shí)間。舉例來(lái)說(shuō),使用cat去讀取 ~/.bashrc,就會(huì)更新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參數(shù)里沒(méi)有--mtime這個(gè)參數(shù),因?yàn)槲覀兡J(rèn)通過(guò)ls -l查看到的時(shí)間就是mtime 。
2.修改文件的時(shí)間
如果需要修改上述三個(gè)時(shí)間,使用touch命令來(lái)修改,touch filename,如果文件不存在,則新建一個(gè)文件.
- $ touch --help
- Usage: touch [OPTION]... FILE...
- Update the access and modification times of each FILE to the current time.
- -a change only the access time
- 修改訪(fǎng)問(wèn)時(shí)間
- -c, --no-create do not create any files
- 修改文件三個(gè)時(shí)間,不存在則不創(chuàng)建
- -d, --date=STRING parse STRING and use it instead of current time
- 指定時(shí)間代替當(dāng)前時(shí)間
- -f (ignored)
- -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
- 指定修改時(shí)間
例如:
- $ touch -d "2 days ago" uconv.h
- $ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
- -rw-rw-r-- 1 work work 1808 Jun 13 18:17 uconv.h
- -rw-rw-r-- 1 work work 1808 Jun 13 18:17 uconv.h
- -rw-rw-r-- 1 work work 1808 Jun 15 18:17 uconv.h
將mtime和atime修改為兩天前,ctime沒(méi)變.
- $ touch -t 201406142020 uconv.h
- $ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
- -rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h
- -rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h
- -rw-rw-r-- 1 work work 1808 Jun 15 18:23 uconv.h
atime和mtime都變了,但是ctime變成了當(dāng)前時(shí)間,使用cp命令,-a保持原屬性.
- $ cp -a uconv.h uconv.h1
- $ ll uconv.h1 ; ll --time=atime uconv.h1 ; ll --time=ctime uconv.h1 ;
- -rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h1
- -rw-rw-r-- 1 work work 1808 Jun 15 18:25 uconv.h1
- -rw-rw-r-- 1 work work 1808 Jun 15 18:27 uconv.h1
mtime和atime都保持原文件不變,但是ctime變成當(dāng)前時(shí)間.
注:如果touch后面接一個(gè)已經(jīng)存在的文件,則該文件的3個(gè)時(shí)間,atime/ctime/mtime,都會(huì)更新為當(dāng)前時(shí)間,若該文件不存在,則會(huì)主動(dòng)建立一個(gè)新的空文件.
- [root@web10 ~]# touch install.log
- [root@web10 ~]# stat install.log
- File: “install.log”
- Size: 33386 Blocks: 80 IO Block: 4096 一般文件
- Device: fd00h/64768d Inode: 7692962 Links: 1
- Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
- Access: 2012-07-13 16:21:50.000000000 +0800
- Modify: 2012-07-13 16:21:50.000000000 +0800
- Change: 2012-07-13 16:21:50.000000000 +0800
同樣,使用ls,查看到的結(jié)果也一樣.
- [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
下面再看一個(gè)和touch不相關(guān)的例子:
- [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
因?yàn)槲抑斑\(yùn)行過(guò)這個(gè)命令一次,所以會(huì)出現(xiàn)覆蓋,不過(guò)這個(gè)覆蓋出的好,剛才讓我們看到了atime和ctime的時(shí)間的差別.
我們?cè)倩氐絫ouch利用touch修改文件時(shí)間:
1.同時(shí)修改文件的修改時(shí)間和訪(fǎng)問(wèn)時(shí)間
touch -d "2010-05-31 08:10:30" install.log
2.只修改文件的修改時(shí)間
touch -m -d "2010-05-31 08:10:30" install.log
3.只修改文件的訪(fǎng)問(wèn)時(shí)間
touch -a -d "2010-05-31 08:10:30" install.log
下面再給一個(gè)rootkit木馬常用的伎倆,就是把后一個(gè)文件的時(shí)間修改成和前一個(gè)相同.
touch -acmr /bin/ls /etc/sh.conf
另外touch還支持像date命令一樣參數(shù)修改文件時(shí)間:
- [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
最后總結(jié)下常用的文件操作與時(shí)間的關(guān)系:
1、訪(fǎng)問(wèn)時(shí)間,讀一次這個(gè)文件的內(nèi)容,這個(gè)時(shí)間就會(huì)更新,比如對(duì)這個(gè)文件使用more命令,ls、stat命令都不會(huì)修改文件的訪(fǎng)問(wèn)時(shí)間.
2、修改時(shí)間,對(duì)文件內(nèi)容修改一次,這個(gè)時(shí)間就會(huì)更新,比如:vim后保存文件,ls -l列出的時(shí)間就是這個(gè)時(shí)間.
3、狀態(tài)改變時(shí)間,通過(guò)chmod命令更改一次文件屬性,這個(gè)時(shí)間就會(huì)更新,查看文件的詳細(xì)的狀態(tài)、準(zhǔn)確的修改時(shí)間等,可以通過(guò)stat命令 文件名.
新聞熱點(diǎn)
疑難解答
圖片精選