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

フォーマットを調整する場合は、出力は OS 固有であり、Linux/Unix では異なるため、マニュアル ページを参照してください。

一般的に、通常のディレクトリリストからも時間を取得できます。

  • ls -lファイルの内容が最後に変更された時刻を出力します。mtime
  • ls -lcファイルステータスの最終変更時刻を出力します。ctime(違いは何ですか?
  • ls -lu最終アクセス時刻を出力するが、atimeこの概念の有用性は議論の対象

もちろん、ctimeファイルが「作成された」時刻は記録されません。POSIX仕様では3つのタイムスタンプしか定義されていませんが、いくつかのLinuxファイルシステム誕生時間/創造時間を保存します。ファイルの作成日を見つけるにはどうすればいいですか?このようなサポートされている構成では、

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

答え2

唯一の三つファイルごとに異なる時間値が保存され、POSIX 標準:http://pubs.opengroup.org/onlinepubs/9699919799/(見る基本定義セクション -> 4. 一般的な概念 -> 4.8 ファイル時刻の更新)

各ファイルには3つの異なるタイムスタンプが関連付けられています。データアクセス、最後のデータの変更、そして、ファイルのステータスが最後に変更されましたこれらの値は、ファイル特性構造体struct statに返されます。<sys/stat.h>

そして<sys/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 で書かれています。Mac stat -xOS 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

ファイルが作成されたばかりのときは、3 つのタイムスタンプは同じです。


1. アタイム

まず、アクセスファイルのデータを読み込む (lessまたはvim)、印刷する ( 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. 時間

さて変化ファイルの状態を変更するには、権限を変更する ( 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. mtime

最後に、修正するファイルを編集してファイルの内容を変更します。

$ 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. 出生時間

また、新しいバージョンstatstat --version 8.13Ubuntu 12.04など)では4番目のタイムスタンプがあることに注意してください。出生時間(ファイル作成時間)。現時点では正しい時間が表示されない可能性があります。

$ 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: -

関連情報