それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

指定したファイルの日付を取得してフォーマットするシェルコマンドdate

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 は便利だと思う。