一天一點學習Linux之atime,mtime,ctime

來源:互聯網
上載者:User

 

其實這個內容本不想拿出來單獨講的,但考慮到一些朋友可能對這三個時間不是很明白,所以就和大家再一起來學習一下。

 

在Linux系統下,檔案的時間有三種記錄方式

mtime (modification time ):在寫入檔案時隨檔案內容的更改而更改的時間。我們用ls -l看到的時間,就是mtime

ctime (status time):是在寫入檔案、更改所有者、許可權或連結設定時隨Inode的內容更改而更改的時間。相當於ls -l –time=ctime所看到的時間

atime (access time):讀取檔案或者執行檔案時更改的時間。也就是用ls -l –time=atime看到的時間

 

可以通過下面的三個例子對比來研究

ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log

具體的內容就不在這裡講述了,大家仔細理解,我想沒有什麼難度的。

正是由於這些時間的關係,有可能某個檔案的時間與目前系統的時間有差距。因為全世界的時間是不一樣的。

上面三個時間定義也很簡單,關鍵是要理解,加深理解的辦法就是自己實踐。OK,下面我們來給大家介紹個touch命令,他作用就是用來”改變檔案或目錄時間命令”。

 

改變檔案或目錄時間命令touch

參數說明

-a :修改atime

-m :修改mtime

-c :僅修改檔案的時間(三個時間一起修改),若該檔案不存在則不建立新的檔案

-d :後面可以接想修改的日期而不用目前的日期,也可以使用 –date=”日期或時間”

-t :後面可以接想修改是時間而不用目前的時間,格式為[YYMMDDhhmm]

 

我們來看個例子

[root@yufei ~]# ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log

-rwxr–r–. 1 root root 31537 Jan 20 05:09 install.log

-rwxr–r–. 1 root root 31537 Feb 9 19:25 install.log

-rwxr–r–. 1 root root 31537 Feb 11 16:28 install.log

[root@yufei ~]# touch -c install.log

[root@yufei ~]# ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log

-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log

-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log

-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log

在預設的狀態下,如果touch後面有接檔案,則該檔案的三個時間(atime/ctime/mtime)都會更新為目前的時間。若該檔案不存在,則會主動的建立一個新的空的檔案,與-c參數不同,-c不建立檔案哦!這個可以自己touch一個檔案出來看一下。

 

我們再來看一個例子

[root@yufei ~]# cp -a /etc/profile ./

[root@yufei ~]# ls -l profile; ls -l –time=atime profile; ls -l –time=ctime profile

-rw-r–r–. 1 root root 1459 Jun 30 2010 profile

-rw-r–r–. 1 root root 1459 Feb 11 10:51 profile

-rw-r–r–. 1 root root 1459 Feb 11 20:46 profile

第一行為此檔案的mtime,也就是這個檔案建立或編輯的時間

第二行為此檔案的atime,這個時間是記錄此檔案的最後訪問時間

第三行為此檔案的ctime,因為這個檔案是剛複製過來的,此檔案的I節點與原檔案肯定不一樣了

[root@yufei ~]# ls -li /etc/profile;ls -li profile

651556 -rw-r–r–. 1 root root 1459 Jun 30 2010 /etc/profile

522034 -rw-r–r–. 1 root root 1459 Jun 30 2010 profile

希望通過上面的講解,大家能對這三個時間能明白了。

從上面的例子我們可以看出,即使我們複製一個檔案時,複製所有的屬性,但也沒有辦法複製ctime這個屬性的。ctime可以記錄這個檔案最近的狀態(status)被改變的時間。

 

如果想改變時間,我們可以通過下面的方法來實現

如將剛才的檔案,日期和時間改成”2010/11/22 8:08″

[root@yufei ~]# touch -t 1011220808 profile

[root@yufei ~]# ls -l profile; ls -l –time=atime profile; ls -l –time=ctime profile

-rw-r–r–. 1 root root 1459 Nov 22 08:08 profile

-rw-r–r–. 1 root root 1459 Nov 22 08:08 profile

-rw-r–r–. 1 root root 1459 Feb 11 21:02 profile

我們看到atime與mtime都改變了,但ctime則是記錄的是系統目前的時間。

 

講了這麼多,其實touth常用的功能就只有兩個

1、建立一個空的檔案

2、修改時間(mtime atime)不過,這個功能用到的也不多

 

希望通過上面的講解,大家能對這三個時間能有個深刻的理解了。

摘自 羽飛的部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.