lsでいろいろソートを試してみました。
方法 | コマンド |
---|---|
アルファベット順 | ls |
タイムスタンプ順 | ls -t |
逆順 | ls -r |
ファイルサイズ順 | ls -S |
拡張子順 | ls -X |
ソートしない | ls -f |
縦に表示する | ls | cat |
所有者のアルファベット順にソート | ls -l | sort -k 3 |
以下実行例です。
詳細情報表示のため-lも合わせてつけておりますが、なくてもソートはできます。
アルファベット順にソート
デフォルトでアルファベット順にソートされている
ls -l
タイムスタンプ順にソート
ls -lt
$ ls -lt 合計 16 -rw-r--r--. 1 root root 15 6月 20 21:02 2015 atest1.txt -rw-r--r--. 1 root root 1280 6月 20 21:00 2015 test3.php -rw-r--r--. 1 root root 180 6月 20 21:00 2015 test2.log -rw-r--r--. 1 root root 15 6月 20 20:59 2015 test1.txt
その他、タイムスタンプで検索したくなったら時はfindのmtimeで検索するとよいと思います。
逆順にソート
以下の場合はアルファベットの逆順にソートされる
ls -lr
$ ls -lr 合計 16 -rw-r--r--. 1 root root 1280 6月 20 21:00 2015 test3.php -rw-r--r--. 1 root root 180 6月 20 21:00 2015 test2.log -rw-r--r--. 1 root root 15 6月 20 20:59 2015 test1.txt -rw-r--r--. 1 root root 15 6月 20 21:02 2015 atest1.txt
ファイルサイズ順にソートする
ls -lS
$ ls -lS 合計 12 -rw-r--r--. 1 root root 1280 6月 20 21:00 2015 test3.php -rw-r--r--. 1 root root 180 6月 20 21:00 2015 test2.log -rw-r--r--. 1 root root 15 6月 20 20:59 2015 test1.txt
拡張子のアルファベット順にソート
ls -lX
$ ls -lX 合計 12 -rw-r--r--. 1 root root 180 6月 20 21:00 2015 test2.log -rw-r--r--. 1 root root 1280 6月 20 21:00 2015 test3.php -rw-r--r--. 1 root root 15 6月 20 20:59 2015 test1.txt
ソートしない
ls -U
もしくはls -f
デフォルトはアルファベット順にソートしています。
このオプションでソートしなくなります。
lsの処理が速くなるメリットがあるようです。
縦に表示する
ls | cat
$ ls | cat
atest1.txt
test1.txt
test2.log
test3.php
所有者順にソートする
sortコマンドと組み合わせて所有者のアルファベット順にソートしてみました。
$ ll | sort -k 3 合計 16 -rw-r--r--. 1 ats ats 15 6月 20 21:02 2015 atest1.txt -rw-r--r--. 1 ats ats 1280 6月 20 21:00 2015 test3.php -rw-r--r--. 1 root root 15 6月 20 20:59 2015 test1.txt -rw-r--r--. 1 root root 180 6月 20 21:00 2015 test2.log