[Linux] DEBUGFS查看檔案的建立時間 & [Mac] GetFileInfo

一般來說用 ls 就可以查到檔案的建立時間,或是使用 stat 指令也可以查到,那到底為什麼還要特地使用別的指令來檢查呢?
有個小原因就是這兩個指令查到的時間都不一定是完全正確的,因為可以修改,
在Linux的環境中,時間大概有下面4種類型

  • ctime: Shows file change time.

    ctime會根據檔案的內容、所有權、屬性被變更時而改變。

  • atime: Shows file access time.

  • mtime: Shows file modification time.
  • crtime: Shows file creation time.

這4種分別就是代表建立(Create/Birth)、修改(Modify/mtime)、存取(Access/atime)及變更(Change/ctime),但建立日期在Linux上並沒有被儲存起來(應該說在檔案系統上沒有辦法很直觀的看到),因此,可以查到和修改的,就只有後面的三種類型,這三種中的修改(mtime)和存取(atime)日期可以用 touch 指令來改變,而crtime就比較難以變更。

- 變更檔案的 atime/mtime 為目前的日期和時間
# touch test.txt

- 變更檔案的 atime 為目前的日期和時間
# touch -a test.txt

- 變更檔案的 mtime 為目前的日期和時間
# touch -m test.txt

- 變更檔案的 atime/mtime 為指定日期,只指定日期,時間會變成000000
# touch -d 20150101 test.txt

- 變更檔案的 atime/mtime 為指定日期和時間(2018/1/1 01:11:22)
# touch -t 201801010111.22 atd.txt

- 將 test.txt 檔案的日期和時間套到 test2.txt 檔案,但 ctime 不會被套用
# touch -r test.txt test2.txt

- 檢視檔案的各種日期和時間
# stat test.txt

- 用 ls 指令查看檔案的 mtime
# ls -l

- 用 ls 指令查看檔案的 ctime
# ls -lc

- 用 ls 指令查看檔案的 atime
# ls -lu

使用DEBUGFS來查檔案的正確建立時間:

可以用 stat 指令來查出該檔案在inode表上的位置值,

# stat test.txt
or
# ls -i test.txt

再來是確認檔案所在的裝置位子,如果不確定檔案在哪個裝置上,可以用以下指令,

# df -t test.txt

再把查到的inode值和位置資料輸入到以下的指令中,假設查到的inode值是54692346

debugfs -R 'stat ' 
    debugfs -R 'stat <54692346>' /dev/xvda1 

就可以找到檔案的crtime了,

ctime: 0x579ed684:8fd54a34 -- Mon Jan  1 10:26:36 2018
atime: 0x58aea120:3ec8dc30 -- Thu Feb 23 14:15:20 2018
mtime: 0x5628ae91:38568be0 -- Thu Jan 22 15:08:25 2018
crtime: 0x579ed684:8fd54a34 -- Mon May  1 10:26:36 2018

另外DEBUGFS是Linux上用來查看 Ext File System 用的指令,所以在Mac上就沒有這樣的指令可以用,反倒是Mac上的 GetFileInfo 可以使用而且比Linux容易多了呢。

# GetFileInfo test.txt
created: 08/10/2017 12:05:20
modified: 08/10/2017 12:05:20

Reference

Ubuntu 用指令變更及檢視檔案修改日期

DEBUGFS Command – Show File Creation Times in Linux

OSX - How to get the creation & modification time of a file from the command line

Add a Comment