lsコマンドでよく使うオプション8選
普段何気なく使うlsコマンド。今回はlsコマンドでよく使うオプションを8つ後紹介していきます。
目次
その1 「-a」オプション:すべてのファイルを表示
「-a」オプションで、「.(ドット)」から始まる隠しファイルも含めてすべてのファイルを表示します。
1 2 3 |
# ls -a . .. .ICE-unix a.txt b.log c.txt d.log e # |
その2 「-l」オプション:ファイルの詳細を表示
「-l」オプションで各ファイルの詳細情報を一ファイル一行で表示します。
1 2 3 4 5 6 7 8 |
# ls -l total 20 -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 884 Mar 18 2013 d.log drwxr-xr-x 2 root root 4096 Oct 17 21:12 e # |
その3 「-t」オプション:更新時間順に並べて表示
「-t」オプションで各ファイルを更新時間順に並べて表示します。
「-lt」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 11 |
# ls -t e d.log c.txt a.txt b.log # # ls -lt total 20 drwxr-xr-x 2 root root 4096 Oct 17 21:12 e -rw-r--r-- 1 root root 884 Mar 18 2013 d.log -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt -rw-r--r-- 1 root root 370 Jan 12 2010 b.log # |
その4 「-S」オプション:ファイルサイズ順に並べて表示
「-S」オプションで各ファイルをファイルサイズ順に並べて表示します。
「-lS」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 |
# ls -S e c.txt d.log b.log a.txt # ls -lS total 20 drwxr-xr-x 2 root root 4096 Oct 17 21:12 e -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 884 Mar 18 2013 d.log -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt # |
ディレクトリのサイズは以下では4096バイトになっていますが、そのサイズで並び替えられます。
1 2 3 4 5 6 7 8 9 |
# ls -lS total 6500 -rw-r--r-- 1 root root 6634579 Feb 20 2015 h.txt drwxr-xr-x 2 root root 4096 Oct 17 21:12 e -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 884 Mar 18 2013 d.log -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt # |
その5 「-r」オプション:逆順で表示
「-r」オプションで各ファイルを逆順で表示します。
「-lr」や「-ltr」、「-lSr」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 11 |
# ls -r e d.log c.txt b.log a.txt # # ls -lr total 20 drwxr-xr-x 2 root root 4096 Oct 17 21:12 e -rw-r--r-- 1 root root 884 Mar 18 2013 d.log -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt # |
その6 「-X」オプション:ファイルを拡張子ごとにまとめて表示
「-X」オプションで各ファイルを拡張子ごとにまとめて表示します。
「-lX」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 11 12 |
# ls -X e b.log d.log a.txt c.txt # # # ls -lX total 20 drwxr-xr-x 2 root root 4096 Oct 17 21:12 e -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 884 Mar 18 2013 d.log -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt # |
その7 「-R」オプション:ディレクトリ内容をサブディレクトリの内容も含めて表示
「-R」オプションでディレクトリ内用をサブディレクトリの内容も含めて表示します。
「-lR」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# ls -R .: a.txt b.log c.txt d.log e ./e: f.txt g.txt # # # ls -lR .: total 20 -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 884 Mar 18 2013 d.log drwxr-xr-x 2 root root 4096 Oct 17 21:12 e ./e: total 8 -rw-r--r-- 1 root root 789 May 20 2009 f.txt -rw-r--r-- 1 root root 460 Jan 12 2010 g.txt # |
その8 「-h」オプション:単位を読みやすくして表示
「-h」オプションで各ファイルのサイズを単位付きで読みやすく表示します。
「-lh」として使用することが多いです。
1 2 3 4 5 6 7 8 9 10 11 |
# ls -h a.txt b.log c.txt d.log e # # ls -lh total 20K -rw-r--r-- 1 root root 158 Jan 12 2010 a.txt -rw-r--r-- 1 root root 370 Jan 12 2010 b.log -rw-r--r-- 1 root root 969 Feb 22 2013 c.txt -rw-r--r-- 1 root root 884 Mar 18 2013 d.log drwxr-xr-x 2 root root 4.0K Oct 17 21:12 e # |
以上です。