隨筆-314  評論-209  文章-0  trackbacks-0

          一、查看文件時間及相關命令

          1、stat查看文件時間

          [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

          說明: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.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 ,查看到的結果也一樣。

          [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命令 文件名。

          posted on 2015-12-21 15:54 xzc 閱讀(2576) 評論(1)  編輯  收藏

          評論:
          # re: linux下查看和修改文件時間 2015-12-21 15:54 | xzc
          linux的終端上,沒有windows的搜索那樣好用的圖形界面工具,但find命令確是很強大的。

            比如按名字查找一個文件,可以用 find / -name targetfilename 。 唉,如果只知道名字,不知道地點,這樣也不失為一個野蠻有效的方法。

            按時間查找也有參數 -atime 訪問時間 -ctime 改變狀態的時間 -mtime修改的時間。但要注意,這里的時間是以24小時為單位的。查看man手冊后使用,你會很迷惑: -mtime n: Files data was last modified n*24 hours ago. 字面上的理解是最后一次修改發生在n個24小時以前的文件,但實際上

            find ./ -mtime 0:返回最近24小時內修改過的文件。

            find ./ -mtime 1 : 返回的是前48~24小時修改過的文件。而不是48小時以內修改過的文件。

            那怎么返回10天內修改過的文件?find還可以支持表達式關系運算,所以可以把最近幾天的數據一天天的加起來:

            find ./ -mtime 0 -o -mtime 1 -o -mtime 2 ……雖然比較土,但也算是個方法了。

            還有沒有更好的方法,我也想知道。

            另外, -mmin參數-cmin / - amin也是類似的。

            回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 霞浦县| 浦北县| 天水市| 陇西县| 洛南县| 翁源县| 赞皇县| 宁海县| 西昌市| 浮山县| 鹤壁市| 商城县| 安仁县| 礼泉县| 西宁市| 伊吾县| 东光县| 比如县| 博湖县| 崇左市| 乳山市| 托克托县| 五原县| 英吉沙县| 大埔县| 桦南县| 武宣县| 乌拉特中旗| 确山县| 长治市| 丰顺县| 宁强县| 双流县| 兴和县| 宁阳县| 普格县| 凯里市| 金秀| 社会| 阳高县| 万年县|