date コマンドでファイルの日付を取得する。
指定したファイルの日付を取得するには
$ date +'%F %T' -r /etc/aliases 2015-08-02 13:35:20
最終更新日が取得できる。
-r, --reference=FILE display the last modification time of FILE
もちろん stat でも構わない。
$ stat -c %y /etc/aliases 2016-10-16 01:23:47.000000000 +0900
でもファイル日付が合わない。。。。みたいなことがある。
これは、stat は シンボリックリンクをデリファレンスせずに、symlink を読み取るので注意が必要。
date -r
と同等のstat
を行うには次のようにする。
$ stat -L -c %y /etc/aliases 2015-08-02 13:35:20.000000000 +0900
stat は w/x/y/z とおぼえやすい並びになってる
フォーマット | 意味 |
---|---|
%w | time of file birth, human-readable; - if unknown |
%x | time of last access, human-readable |
%y | time of last data modification, human-readable |
%z | time of last status change, human-readable |
$ stat -c %w /etc/aliases 2016-10-16 01:23:47.000000000 +0900 $ stat -c %x /etc/aliases 2016-10-16 01:23:47.000000000 +0900 $ stat -c %y /etc/aliases 2016-10-16 01:23:47.000000000 +0900 $ stat -c %z /etc/aliases 2016-10-16 01:24:48.000000000 +0900
でも覚えるのめんどくさいので、date -r
は便利だと思う。