LinuxのLSコマンドの容量表示をK,M,Gの単位で見やすくする方法

lsコマンドでファイルを表示すると容量はバイト単位で表示されるため、容量の大きいファイルの表示の場合はどれくらいの容量かが分かりにくいですよね。
lsコマンドのオプションを付けて実行することで、わかりやすい単位で容量を表示する方法を紹介します。

普通にlsコマンドで表示した場合

$ ls -l
total 81724
-rw-rw-r-- 1 ec2-user ec2-user 30352770 Jul 28  2020 fileA.txt
-rw-rw-r-- 1 ec2-user ec2-user 13540487 Mar  5  2020 fileB.txt
-rw-rw-r-- 1 ec2-user ec2-user 39637574 Mar 14  2020 fileC.txt
-rw-rw-r-- 1 ec2-user ec2-user     6638 Oct 18  2020 fileD.txt
-rw-rw-r-- 1 ec2-user ec2-user     1704 Oct 18  2020 fileE.txt
-rw-rw-r-- 1 ec2-user ec2-user   127809 Jan  7  2020 fileF.txt

容量をわかりやすい単位で表示

KbyteやMbyteなど人間が見て見やすい単位に変換する場合は「h」オプションを使用します。

$ ls -lh
total 80M
-rw-rw-r-- 1 ec2-user ec2-user  29M Jul 28  2020 fileA.txt
-rw-rw-r-- 1 ec2-user ec2-user  13M Mar  5  2020 fileB.txt
-rw-rw-r-- 1 ec2-user ec2-user  38M Mar 14  2020 fileC.txt
-rw-rw-r-- 1 ec2-user ec2-user 6.5K Oct 18  2020 fileD.txt
-rw-rw-r-- 1 ec2-user ec2-user 1.7K Oct 18  2020 fileE.txt
-rw-rw-r-- 1 ec2-user ec2-user 125K Jan  7  2020 fileF.txt

容量のサイズ順にソートする

「S」オプションを使用してソートすることで容量の大きい順に表示できます。

$ ls -lhS
total 80M
-rw-rw-r-- 1 ec2-user ec2-user  38M Mar 14  2020 fileC.txt
-rw-rw-r-- 1 ec2-user ec2-user  29M Jul 28  2020 fileA.txt
-rw-rw-r-- 1 ec2-user ec2-user  13M Mar  5  2020 fileB.txt
-rw-rw-r-- 1 ec2-user ec2-user 125K Jan  7  2020 fileF.txt
-rw-rw-r-- 1 ec2-user ec2-user 6.5K Oct 18  2020 fileD.txt
-rw-rw-r-- 1 ec2-user ec2-user 1.7K Oct 18  2020 fileE.txt

容量の小さい順にソートしたい場合は逆順で表示する「r」オプションを使用します。

$ ls -lhSr
total 80M
-rw-rw-r-- 1 ec2-user ec2-user 1.7K Oct 18  2020 fileE.txt
-rw-rw-r-- 1 ec2-user ec2-user 6.5K Oct 18  2020 fileD.txt
-rw-rw-r-- 1 ec2-user ec2-user 125K Jan  7  2020 fileF.txt
-rw-rw-r-- 1 ec2-user ec2-user  13M Mar  5  2020 fileB.txt
-rw-rw-r-- 1 ec2-user ec2-user  29M Jul 28  2020 fileA.txt
-rw-rw-r-- 1 ec2-user ec2-user  38M Mar 14  2020 fileC.txt

Leave a Reply

Your email address will not be published. Required fields are marked *