Linuxでlsコマンドの結果をソートする方法(サイズ、更新日付、拡張子/昇順・降順)

目次

スポンサードリンク

Linuxコマンドには、ファイルの一覧を取得するコマンド「ls」があります。最もタイプすることが多いコマンドだと思いますが このコマンドには出力内容をソートする各種オプションが用意されています。この記事ではよく利用するコマンドを紹介します。

ls コマンドの結果をソートするオプション

lsコマンドはオプションなしだとアルファベット順で結果をソートしますが、オプションを追加することで条件を指定してソートすることができます。いくつか例を紹介します。

※Linuxディストリビューションの種類によっては「ls -l」のエイリアスとして「ll」(アルファベットのl(エル)二つ)が コマンドとして登録されていることがあります。その場合は-lオプションが不要なのでタイプ回数を削減できて便利です。

ファイルサイズで降順ソートするコマンド「ls -lS」

左から5番目の列がファイルサイズです。降順でソートされてることがわかります。

[root@gcsi4 conf.modules.d]# ls -lS
合計 48
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak
-rw-r--r--  1 root root  423  6月  7  2022 15-php.conf
-rw-r--r--  1 root root  151  4月 11  2018 10-mod_security.conf.bak
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--. 1 root root   45 12月  2  2017 10-fcgid.conf.bak
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf

ファイルサイズで昇順ソートするコマンド「ls -lSr」

降順ソートのコマンドオプションの最後にrを追加すると逆順でソートがかかります。

[root@gcsi4 conf.modules.d]# ls -lSr
合計 48
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--. 1 root root   45 12月  2  2017 10-fcgid.conf.bak
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root  151  4月 11  2018 10-mod_security.conf.bak
-rw-r--r--  1 root root  423  6月  7  2022 15-php.conf
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf

タイムスタンプで降順ソートするコマンド「ls -lt」

tオプションはファイルのタイムスタンプで降順ソートしてくれるコマンドオプションです。

[root@gcsi4 conf.modules.d]# ls -lt
合計 48
-rw-r--r--  1 root root  423  6月  7  2022 15-php.conf
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--  1 root root  151  4月 11  2018 10-mod_security.conf.bak
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--. 1 root root   45 12月  2  2017 10-fcgid.conf.bak
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak

タイムスタンプで昇順ソートするコマンド「ls -ltr」

このコマンドでもrオプションを追加すると逆順でソートがかかります。

[root@gcsi4 conf.modules.d]# ls -ltr
合計 48
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak
-rw-r--r--. 1 root root   45 12月  2  2017 10-fcgid.conf.bak
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--  1 root root  151  4月 11  2018 10-mod_security.conf.bak
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  423  6月  7  2022 15-php.conf

ファイルの拡張子順でソートするコマンド「ls -lX」

Xオプションはファイルの拡張子部分でソートします。例を見るとファイル名末尾が.bakのファイルが上位に並んでいることがわかります。

[root@gcsi4 conf.modules.d]# ls -lX
合計 48
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak
-rw-r--r--. 1 root root   45 12月  2  2017 10-fcgid.conf.bak
-rw-r--r--  1 root root  151  4月 11  2018 10-mod_security.conf.bak
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--  1 root root  423  6月  7  2022 15-php.conf

その他、覚えておくと便利なコマンド

出力結果が多すぎる場合はheadコマンドと組み合わせる「ll | head」

llコマンドの出力結果をパイプしheadコマンドにつなぐと最初の10行を表示できる。あまりにも行数が多くて画面に収まりきらないときによく使うコマンド。

[root@gcsi4 conf.modules.d]# ll | head
合計 48
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf
-rw-r--r--  1 root root  957  1月 14  2022 00-proxy.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-ssl.conf
-rw-r--r--  1 root root   88  1月  8  2022 00-systemd.conf
-rw-r--r--  1 root root  451  1月  8  2022 01-cgi.conf
-rw-r--r--. 1 root root  451 10月 20  2017 01-cgi.conf.bak

n オプションを使うと任意の行数を表示することができます。

[root@gcsi4 conf.modules.d]# ll | head -n 5
合計 48
-rw-r--r--  1 root root 3739  1月 14  2022 00-base.conf
-rw-r--r--  1 root root  139  1月  8  2022 00-dav.conf
-rw-r--r--  1 root root   41  1月  8  2022 00-lua.conf
-rw-r--r--. 1 root root  926  4月  3  2018 00-mpm.conf

行数をカウントしたい場合はwcコマンドと組み合わせる「ll | wc -l」

llコマンドの出力が何行あるか知りたいときはwcコマンドにパイプでつなぐといい。wcコマンドのlオプションは標準入力の何行あるかが出力されます。ll結果の先頭の「合計 XX」の表示が含まれるのは注意。

[root@gcsi4 conf.modules.d]# ll | wc -l
13

リナックスコマンドの知識は、プログラマにとって長く役立つ知識です。 私はこちらの書籍で一通り知識を抑えました。基本から丁寧に解説されています。

関連記事