atime:Access Time 最後一次訪問檔案(讀取或執行)或目錄的時間
mtime:Modofy Time 最後一次修改檔案(內容)或目錄(內容)的時間
ctime:Change Time 最後一次改變檔案(屬性)或目錄(屬性)的時間
如何查看檔案或目錄的atime/mtime/ctime
1、stat filename
2、
atime: ls -lu filename
mtime: ls -l filename
ctime: ls -lc filename
樣本示範
在/tmp下建立一個目錄tm,tm下建立一個檔案a.txt
| 代碼如下 |
複製代碼 |
# cd /tmp
# mkdir tm
# cd tm
# stat ../tm
File: `../tm'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: ca01h/51713d Inode: 1050207 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 10:57:32.993350411 +0800
Modify: 2014-03-17 10:57:32.993350411 +0800
Change: 2014-03-17 10:57:32.993350411 +0800
Birth: -
# touch a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 10:58:01.328805868 +0800
Modify: 2014-03-17 10:58:01.328805868 +0800
Change: 2014-03-17 10:58:01.328805868 +0800
Birth: -
|
新建立的目錄和檔案的atime/mtime/ctime都是一樣的。
開啟a.txt,隨便輸入焦點內容然後儲存。
| 代碼如下 |
複製代碼 |
# vi a.txt
# stat a.txt
File: `a.txt'
Size: 3 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:02:22.423782143 +0800
Birth: -
|
vi a.txt的時候會修改檔案的atime,輸入內容儲存後會修改mtime,同時因為檔案的屬性變更了(比如檔案大小就變了),所以ctime也會改變。
修改檔案的屬性,ctime變化,mtime和atime不變。
| 代碼如下 |
複製代碼 |
# chmod g+w a.txt
# stat a.txt
File: `a.txt'
Size: 9 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1050208 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:07:10.114234176 +0800
Birth: -
|
修改內容一定會改變atime嗎?答案是否!
| 代碼如下 |
複製代碼 |
|
# echo 'world' >> a.txt
# stat a.txt
File: `a.txt'
Size: 9 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:09:07.980591047 +0800
Change: 2014-03-17 11:09:07.980591047 +0800
Birth: - |
touch指令的作用就是用於改變檔案的時間戳記,touch命令的文法格式如下:
touch [選項]... filename...
選項與參數:
-a : 僅修訂 atime;
-c : 僅修改檔案的時間,若該檔案不存在則不建立新檔案;
-d : 後面可以接欲修訂的日期而不用目前的日期,也可以使用 --date="日期或時間"
-m : 僅修改 mtime ;
-t : 後面可以接欲修訂的時間而不用目前的時間,格式為[YYMMDDhhmm]
-r : 把指定文檔或目錄的日期時間,統統設成和參考文檔或目錄的日期時間相同
...
樣本示範
| 代碼如下 |
複製代碼 |
# touch a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050217 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-18 09:30:49.965240705 +0800
Modify: 2014-03-18 09:30:49.965240705 +0800
Change: 2014-03-18 09:30:49.965240705 +0800
Birth: -
|
先通過touch建立a.txt,如果已經存在則會修改檔案的atime/mtime/ctime。
| 代碼如下 |
複製代碼 |
# touch a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050217 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-18 09:31:43.584234285 +0800
Modify: 2014-03-18 09:31:43.584234285 +0800
Change: 2014-03-18 09:31:43.584234285 +0800
Birth: -
|
再touch a.txt,發現atime/mtime/ctime都發生變化了。
| 代碼如下 |
複製代碼 |
# touch -a a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050217 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-18 09:32:01.459898755 +0800
Modify: 2014-03-18 09:31:43.584234285 +0800
Change: 2014-03-18 09:32:01.459898755 +0800
Birth: -
|
使用touch -a a.txt,只有atime和ctime發生變化了,mtime儲存不變。
| 代碼如下 |
複製代碼 |
# touch -m a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050217 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-18 09:32:01.459898755 +0800
Modify: 2014-03-18 09:32:22.591502109 +0800
Change: 2014-03-18 09:32:22.591502109 +0800
Birth: -
|
使用touch -m a.txt,只有mtime和ctime發生變化了,atime儲存不變。
任何情況下,使用touch指令,檔案的ctime都會發生改變。以上經過touch指令後,atime/mtime/ctime的值都是目前時間點的值,通過touch指令還可以隨意指定時間戳記。
| 代碼如下 |
複製代碼 |
# touch -d "2 days ago" b.txt && stat b.txt
File: `b.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050218 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-16 09:38:49.261034132 +0800
Modify: 2014-03-16 09:38:49.261034132 +0800
Change: 2014-03-18 09:38:49.256243241 +0800
Birth: -
|
touch -d 通過指定一個表示時間的字串來當做目前時間,這個格式比較自由,只要是能表示時間的都可以,例如”2014-02-28 16:21:42″、”next Thursday”、”2 days ago”等。
| 代碼如下 |
複製代碼 |
# touch -t 201401011212.30 c.txt && stat c.txt
File: `c.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050219 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-01-01 12:12:30.000000000 +0800
Modify: 2014-01-01 12:12:30.000000000 +0800
Change: 2014-03-18 09:39:45.983178168 +0800
Birth: -
# touch -t 01011212 d.txt && stat d.txt
File: `d.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050220 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-01-01 12:12:00.000000000 +0800
Modify: 2014-01-01 12:12:00.000000000 +0800
Change: 2014-03-18 09:41:59.092678768 +0800
Birth: -
|
touch -t 指令後面的時間戳記格式為:[[CC]YY]MMDDhhmm[.ss]
同樣的,ctime始終是目前時間
ctime,mtime,atime的區別
當你同熟練的UNIX使用者進行交談時,你經常會聽到他們傲慢地講出術語“改變時間(change time)”和“修改時間(modification time)”。對於許多人(和許多字典而言),改變和修改是相同的。這裡會有什麼不同那?
改變和修改之間的區別在於是改某個組件的標籤還是更改它的內容。如果有人說chmod a-w myfile,那麼這是一個改變;如果有人說echo foo >> myfile,那麼
這是一個修改。改變是檔案的索引節點發生了改變;修改是文本本身的內容發生了變化。[檔案的修改時間也叫時間標誌 (timestamp).]
只要討論改變時間和修改時間,就不可能不提到“訪問時間(access time)”.訪問時間是檔案最後一次被讀取的時間。因此閱讀一個檔案會更新它的訪問時間,當它的改變時間並沒有變化(有關檔案的資訊沒有被改變),它的修改時間也同樣如此(檔案本身沒有被改變)
有時,在許多地方改變時間或者“ctime”被錯誤地寫成“建立時間”,包括某些UNIX參考手冊。不要相信他們
下面是我man出來的內容,僅供參考!
| 代碼如下 |
複製代碼 |
|
st_atime
Time when file data was last accessed. Changed by the
following functions: creat(), mknod(), pipe(),
utime(2), and read(2).
st_mtime
Time when data was last modified. Changed by the fol-
lowing functions: creat(), mknod(), pipe(), utime(),
and write(2).
st_ctime
Time when file status was last changed. Changed by the
following functions: chmod(), chown(), creat(),
link(2), mknod(), pipe(), unlink(2), utime(), and
write(). |
ls顯示出的time應該是mtime。
touch後,檔案的三個時間應該都會改變,可以試一試。
問題描述
檔案的 ctime、mtime、atime 之間有什麼區別?
配置資訊
解決方案
檔案的 Access time,atime 是在讀取檔案或者執行檔案時更改的。
檔案的 Modified time,mtime 是在寫入檔案時隨檔案內容的更改而更改的。
檔案的 Create time,ctime 是在寫入檔案、更改所有者、許可權或連結設定時隨 Inode 的內容更改而更改的。
因此,變更檔的內容即會更改 mtime 和 ctime,但是檔案的 ctime 可能會在 mtime 未發生任何變化時更改 - 在許可權更改,但是檔案內容沒有變化的情況下。
ls(1) 命令可用來列出檔案的 atime、ctime 和 mtime。
ls -lc filename 列出檔案的 ctime
ls -lu filename 列出檔案的 atime
ls -l filename 列出檔案的 mtime
atime不一定在訪問檔案之後被修改,因為:使用ext3檔案系統的時候,如果在mount的時候使用了noatime參數那麼就不會更新atime的 資訊。而這是加了 noatime 取消了, 不代表真??情?r.反正, ?三?? time stamp 都放在 inode 中.若 mtime, atime 修改, inode 就一定??? 既然 inode 改了, 那 ctime 也就跟著要改了.之所以在 mount option 中使用 noatime, 就是不想 file system 做太多的修改, 而改善?取效能.