💭

Linuxでファイル一覧をファイル名だけでタイムスタンプ順に縦に出力したい

2023/01/02に公開

Linux でファイル名を出力するときにlsコマンドを使いますが、

❯ ls
sample.txt  sample2.txt  sample3.txt  sample4.txt

縦に表示しようとしてls -lとすると、-lが詳細表示のオプションなのでユーザー名やグループ、パーミッションも出力されます。

❯ ls -l
合計 0
-rw-r--r-- 1 user user 0 12月 31 00:00 sample.txt
-rw-r--r-- 1 user user 0  1月  1 00:00 sample2.txt
-rw-r--r-- 1 user user 0  1月  2 00:00 sample3.txt
-rw-r--r-- 1 user user 0  1月  3  2023 sample4.txt

更新日時順にファイル名だけを出力したい場合は、ls -t1とすると、下記のように出力できます。-tと数字の1の組み合わせです。

❯ ls -t1
sample4.txt
sample3.txt
sample2.txt
sample.txt

それぞれのオプションの意味は
-1 1 ファイル 1 行で表示する
-t ファイル更新時間で新しい順にソートする
です。

Discussion