Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 檔案的三種時間戳完全解析

Linux 相關職位的面試中,檔案時間經常被考問。Linux 檔案有三種時間:Access time(atime,訪問時間)、Modification time(mtime,內容修改時間)、Status change time(ctime,狀態變更時間)。

查詢方式:

ls -l     # 顯示 mtime,內容最後修改時間
ls -lu    # 顯示 atime,最後訪問時間
ls -lc    # 顯示 ctime,屬性最後變更時間

以下以 Ubuntu 10.04 為平台逐步示範:

1. 建立檔案後,三個時間一致

Jacob@server01:~/test$ touch test.txt
Jacob@server01:~/test$ ls -l
-rw-r--r-- 1 Jacob Jacob 0 2012-12-04 09:29 test.txt   # mtime 09:29
Jacob@server01:~/test$ ls -lu
-rw-r--r-- 1 Jacob Jacob 0 2012-12-04 09:29 test.txt   # atime 09:29
Jacob@server01:~/test$ ls -lc
-rw-r--r-- 1 Jacob Jacob 0 2012-12-04 09:29 test.txt   # ctime 09:29

2. 修改內容後,mtime 與 ctime 更新,atime 不變

Jacob@server01:~/test$ echo "abc" > test.txt
Jacob@server01:~/test$ ls -l
-rw-r--r-- 1 Jacob Jacob 4 2012-12-04 09:31 test.txt   # mtime 09:31
Jacob@server01:~/test$ ls -lc
-rw-r--r-- 1 Jacob Jacob 4 2012-12-04 09:31 test.txt   # ctime 09:31
Jacob@server01:~/test$ ls -lu
-rw-r--r-- 1 Jacob Jacob 4 2012-12-04 09:29 test.txt   # atime 仍為 09:29

3. 修改屬性(權限)後,僅 ctime 更新

Jacob@server01:~/test$ chmod a+x test.txt
Jacob@server01:~/test$ ls -l
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:31 test.txt   # mtime 09:31
Jacob@server01:~/test$ ls -lu
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:29 test.txt   # atime 09:29
Jacob@server01:~/test$ ls -lc
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:33 test.txt   # ctime 09:33

4. 讀取內容後,僅 atime 更新

Jacob@server01:~/test$ cat test.txt
abc
Jacob@server01:~/test$ ls -l
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:31 test.txt   # mtime 09:31
Jacob@server01:~/test$ ls -lu
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:35 test.txt   # atime 09:35
Jacob@server01:~/test$ ls -lc
-rwxr-xr-x 1 Jacob Jacob 4 2012-12-04 09:33 test.txt   # ctime 09:33

小結:mtime 追蹤內容變化,ctime 追蹤屬性(含權限、連結數等)變化,atime 追蹤最後一次讀取。三者含義不同,面試時切勿混淆。

Copyright © Linux教程網 All Rights Reserved