知に至る病

お勉強したことを忘れないように書き留めています。

ls コマンドのタイムスタンプの書式を指定する

--time-style オプションで ls -l のタイムスタンプの書式を指定できることを知りました。 これまでずっと更新日時の情報がわかりづらいと不満だったのですが,このオプションの存在を知ってストレスが大幅に軽減されました。 そりゃ指定できますよね……。

タイムスタンプの書式

デフォルトでは ls -l で表示されるタイムスタンプは以下のようになります。 直近 6 ヵ月以内のファイルは月日時分表示,それ以外は過去のファイルも未来のファイルも年月日表示です。

$ ls -l
合計 3
-rw-r--r-- 1 amano41 amano41 5 1月   2  2000 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 1月  14  2016 mage.txt
-rw-r--r-- 1 amano41 amano41 5 7月  15 16:16 piyo.txt

GNU coreutilsls には --time-style オプションがあり,タイムスタンプの書式を指定することができます。 指定できるのは以下の値です。

意味
full-iso ナノ秒単位まで表示
long-iso 分単位まで表示
iso 直近 6 ヵ月以内は月日時分,それ以外は年月日
locale ロケール依存(ls コマンドのデフォルトと同じ)
+FORMAT date コマンドの書式指定子で指定する

タイムスタンプの書式は date コマンドと同じやり方で自由に指定することができます。 たとえば 2016-07-15 01:23:45 というような書式で表示したい場合には,以下のように指定します。

$ ls -l --time-style="+%Y-%m-%d %H:%M:%S"
合計 3
-rw-r--r-- 1 amano41 amano41 5 2000-01-02 03:45:06 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 2016-01-14 16:16:35 mage.txt
-rw-r--r-- 1 amano41 amano41 5 2016-07-15 16:16:39 piyo.txt

出力例

full-iso

$ ls -l --time-style=full-iso
合計 3
-rw-r--r-- 1 amano41 amano41 5 2000-01-02 03:45:06.000000000 +0900 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 2016-01-14 16:16:35.000000000 +0900 mage.txt
-rw-r--r-- 1 amano41 amano41 5 2016-07-15 16:16:39.849838900 +0900 piyo.txt

long-iso

$ ls -l --time-style=long-iso
合計 3
-rw-r--r-- 1 amano41 amano41 5 2000-01-02 03:45 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 2016-01-14 16:16 mage.txt
-rw-r--r-- 1 amano41 amano41 5 2016-07-15 16:16 piyo.txt

iso

$ ls -l --time-style=iso
合計 3
-rw-r--r-- 1 amano41 amano41 5 2000-01-02  hoge.txt
-rw-r--r-- 1 amano41 amano41 5 2016-01-14  mage.txt
-rw-r--r-- 1 amano41 amano41 5 07-15 16:16 piyo.txt

locale

$ ls -l --time-style=locale
合計 3
-rw-r--r-- 1 amano41 amano41 5 1月   2  2000 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 1月  14  2016 mage.txt
-rw-r--r-- 1 amano41 amano41 5 7月  15 16:16 piyo.txt

--full-time オプション

ls -l --time-style=full-iso と同じ出力をする --full-time というオプションもあります。

$ ls --full-time
合計 3
-rw-r--r-- 1 amano41 amano41 5 2000-01-02 03:45:06.000000000 +0900 hoge.txt
-rw-r--r-- 1 amano41 amano41 5 2016-01-14 16:16:35.000000000 +0900 mage.txt
-rw-r--r-- 1 amano41 amano41 5 2016-07-15 16:16:39.849838900 +0900 piyo.txt