1. 時間

1. 時間

Linux有沒有指令可以檢查檔案的所有時間戳記?

我試圖查看文件的最後修改、建立和觸摸日期。

答案1

該命令稱為stat

$ stat test
234881026 41570368 -rw-r--r-- 1 werner staff 0 0 "Feb  7 16:03:06 2012" "Feb  7 16:03:06 2012" "Feb  7 16:03:06 2012" "Feb  7 16:03:06 2012" 4096 0 0 test

如果要調整格式,請參閱手冊頁,因為輸出是特定於作業系統的,並且在 Linux/Unix 下會有所不同。

一般來說,您也可以透過普通的目錄清單來取得時間:

  • ls -l輸出上次修改文件內容的時間mtime
  • ls -lc輸出最後一次文件狀態修改時間,ctime(有什麼不同?
  • ls -lu輸出上次訪問時間atime(儘管這個概念的用處是有待討論

當然,ctime不會記錄文件何時“創建”。 POSIX規範只定義了三個時間戳,但是一些 Linux 檔案系統儲存出生時間/創建時間。如何查找文件的建立日期?在這樣一種支援的配置上,可以使用

stat --printf '%n\nmtime: %y\nctime: %z\natime: %x\ncrtime:%w\n'

答案2

只有為每個文件儲存不同的時間值,由POSIX標準:http://pubs.opengroup.org/onlinepubs/9699919799/(看基本定義部分 -> 4. 一般概念 -> 4.8 文件時間更新)

每個檔案都有三個不同的關聯時間戳記:最後一次的時間資料存取, 最後一次的時間數據修改,以及時間文件狀態上次更改。這些值在檔案特徵結構 struct stat 中傳回,如中所述<系統/stat.h>

並從<系統/stat.h>:

atime is for Last data access timestamp.
mtime is for Last data modification timestamp.
ctime is for Last file status change timestamp.

以下範例顯示了之間的差異阿泰姆,時間時間,這些範例位於 GNU/Linux BASH 中。您可以stat -x在 Mac OS X 或其他 BSD Dist 中使用。查看類似的輸出格式。

$ stat --version
stat (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ touch test
$ stat test
  File: `test'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d  Inode: 98828525    Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank)
Access: 2014-03-16 10:58:28.609223953 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

文件剛創建時,三個時間戳記是相同的。


1. 時間

首先,讓我們使用權透過讀取檔案資料(lessvim)、列印檔案資料(cat)或複製到另一個檔案(cp)。

$ cat test #Nothing will be printed out, since the file is empty
$ stat test
  File: `test'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d  Inode: 98828525    Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank)
Access: 2014-03-16 10:59:13.182301069 +0800  <-- atime Changed!
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

2.ctime

現在讓我們改變檔案狀態,透過變更權限 ( chmod) 或重新命名 ( mv)

$ chmod u+x test
$ stat test
  File: `test'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d  Inode: 98828525    Links: 1
Access: (0764/-rwxrw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:04:10.178285430 +0800  <-- ctime Changed!
$    
$ mv test testing
$ stat testing
  File: `testing'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d  Inode: 98828525    Links: 1
Access: (0764/-rwxrw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:06:33.342207679 +0800  <-- ctime Changed again!

請注意,到目前為止,內容 (數據) 文件的內容仍與創建時相同。


3.時光網

最後,讓我們調整透過編輯文件來取得文件的內容。

$ echo 'Modify the DATA of the file' > testing
$ echo 'Modify the DATA of the file also change the file status' > testing
$ stat testing
  File: `testing'
  Size: 56          Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d  Inode: 98828525    Links: 1
Access: (0764/-rwxrw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 11:09:48.247345148 +0800  <-- mtime Changed!
Change: 2014-03-16 11:09:48.247345148 +0800  <-- ctime also Changed!

4.出生時間

另請注意,較新的版本stat(例如stat --version 8.13在 Ubuntu 12.04 中)有第四個時間戳記 -出生時間(文件創建時間)。雖然現在可能無法顯示正確的時間:

$ stat --version
stat (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ stat birth_time
  File: `birth_time'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 805h/2053d  Inode: 4073946     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ bingyao)   Gid: ( 1000/ bingyao)
Access: 2014-03-16 10:46:48.838718970 +0800
Modify: 2014-03-16 10:46:48.838718970 +0800
Change: 2014-03-16 10:46:48.838718970 +0800
 Birth: -

相關內容