lsコマンドで覚えておきたい使い方16個
Linuxを用いる上で必須とも言えるlsコマンド。
そのコマンドについて、個人的に覚えておきたい使い方についてまとめてみることにした。
内容としては、覚えておきたいオプションと他のコマンドとの組み合わせでの使い方、計16個だ。
なお、例として使用しているのはUbuntu 14.04LTSを用いている。
1.基本の使い方
まずは基本的な使い方から。
オプションを使用しないで使用すると、以下のように出力される。
test@test-vm-ubuntu:~/test$ ls aaa abc abcd abd archey-0.2.8.deb screenfetch-dev test1 test@test-vm-ubuntu:~/test$
リストとして表示させたい場合は、「-l」オプションを付加する。
test@test-vm-ubuntu:~/test$ ls -l 合計 152 -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 11:52 test1 test@test-vm-ubuntu:~/test$
「-l」オプションを用いるとそのファイルの権限や所有者等の情報も表示されるが、ただファイル名のみリスト化したい場合は「-1」を用いると良い。
test@test-vm-ubuntu:~/test$ ls -1 aaa abc abcd abd archey-0.2.8.deb screenfetch-dev test1 test@test-vm-ubuntu:~/test$
隠しファイルやフォルダ(.から始まるもの)を表示させるには、「-a」オプションを付加する。
test@test-vm-ubuntu:~/test$ ls -la 合計 160 drwxrwxr-x 3 test test 4096 7月 12 11:58 . drwxr-xr-x 24 test test 4096 7月 12 11:50 .. -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 11:52 test1 test@test-vm-ubuntu:~/test$
なお、この時「-a」ではなく「-A」を用いると、このフォルダと親フォルダの情報(3行目と4行目)を表示しないでリスト化してくれる。
2.ファイルサイズ順でのソート
ファイルサイズが大きいもの順に並べ替える場合は、「-S」オプションを付加する。
test@test-vm-ubuntu:~/test$ ls -laS 合計 160 -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb drwxrwxr-x 3 test test 4096 7月 12 11:58 . drwxr-xr-x 24 test test 4096 7月 12 11:50 .. drwxrwxr-x 2 test test 4096 7月 12 11:52 test1 -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 0 7月 12 11:51 abd test@test-vm-ubuntu:~/test$
逆順にしたい場合は、「-r」オプションも付加する。
3.時間順でのソート
ファイルサイズだけではなく、ファイル更新時間やアクセス時間順でのソートも可能だ。
ファイル更新時間順でソートする場合は「-t」を、アクセス時間順でソートするのであれば「-tu」オプションを付加すればいい。
逆順にする場合は「-r」オプションを付加すればいい。
test@test-vm-ubuntu:~/test$ ls -lt 合計 152 -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd test@test-vm-ubuntu:~/test$ ls -ltu 合計 152 -rw-rw-r-- 1 test test 0 7月 13 00:08 aaa.xlsx -rw-rw-r-- 1 test test 0 7月 13 00:08 aaa -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xls drwxrwxr-x 2 test test 4096 7月 12 12:13 test1 -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 2 test test 1212 7月 11 07:53 abcd test@test-vm-ubuntu:~/test$
なお、「-u」オプションは「-l」とのみ組み合わせた場合、アクセス時間のみを表示させる。
4.拡張子順でソートする
拡張子順でソートする場合は、「-X」オプションを付加する。
test@test-vm-ubuntu:~/test$ ls -laX 合計 160 -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 drwxrwxr-x 3 test test 4096 7月 13 00:38 . drwxr-xr-x 24 test test 4096 7月 13 00:38 .. -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 0 7月 13 00:38 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx test@test-vm-ubuntu:~/test$
5.ファイルの容量を読みやすくする
通常、「-l」オプションで表示されるファイルの容量はバイト数で表示されるが、「-h」オプションでそれをよりわかりやすい単位で表示させる事ができる。
test@test-vm-ubuntu:~/test$ ls -lh 合計 152K -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1.2K 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6.3K 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 135K 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4.0K 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$
6.ディレクトリの末尾に「/」を追加する
ディレクトリの末尾に「/」を追加するには、「-F」オプションを付加すればいい。
test@test-vm-ubuntu:~/test$ ls -laF 合計 160 drwxrwxr-x 3 test test 4096 7月 13 00:38 ./ drwxr-xr-x 24 test test 4096 7月 13 00:38 ../ -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 0 7月 13 00:38 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1/ test@test-vm-ubuntu:~/test$
7.サブディレクトリ以下の内容も確認する
サブディレクトリ配下も同時にlsを行う場合は、「-R」オプションを付加する。
test@test-vm-ubuntu:~/test$ ls -laR .: 合計 160 drwxrwxr-x 3 test test 4096 7月 13 00:38 . drwxr-xr-x 24 test test 4096 7月 13 00:38 .. -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 0 7月 13 00:38 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 ./test1: 合計 8 drwxrwxr-x 2 test test 4096 7月 12 12:12 . drwxrwxr-x 3 test test 4096 7月 13 00:38 .. -rw-rw-r-- 1 test test 0 7月 12 12:12 test test@test-vm-ubuntu:~/test$
8.ユーザ名、グループ名の表示をユーザID、グループIDに変更する
test@test-vm-ubuntu:~/test$ ls -la 合計 160 drwxrwxr-x 3 test test 4096 7月 13 00:38 . drwxr-xr-x 24 test test 4096 7月 13 00:38 .. -rw-rw-r-- 1 test test 0 7月 12 11:58 .ass -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 0 7月 13 00:38 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$ ls -lan 合計 160 drwxrwxr-x 3 1000 1000 4096 7月 13 00:38 . drwxr-xr-x 24 1000 1000 4096 7月 13 00:38 .. -rw-rw-r-- 1 1000 1000 0 7月 12 11:58 .ass -rw-rw-r-- 1 1000 1000 0 7月 12 11:50 aaa -rw-rw-r-- 1 1000 1000 0 7月 13 00:38 aaa.xls -rw-rw-r-- 1 1000 1000 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 1000 1000 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 1000 1000 1212 7月 11 00:08 abcd -rw-rw-r-- 1 1000 1000 0 7月 12 11:51 abd -rw-rw-r-- 1 1000 1000 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 1000 1000 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 1000 1000 4096 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$
9.時間の表示形式を変更する
「-l」オプションで表示される時間の表示形式を変更させることも出来る。
表示形式の変更は「–time-style=」オプションで行える。
test@test-vm-ubuntu:~/test$ ls -la --time-style=+"%Y%m%d-%H%M%S" 合計 160 drwxrwxr-x 3 test test 4096 20140713-085542 . drwxr-xr-x 24 test test 4096 20140713-085542 .. -rw-rw-r-- 1 test test 0 20140712-115809 .ass -rw-rw-r-- 1 test test 0 20140712-115016 aaa -rw-rw-r-- 1 test test 0 20140713-003844 aaa.xls -rw-rw-r-- 1 test test 0 20140712-121624 aaa.xlsx lrwxrwxrwx 1 test test 17 20140712-115224 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 20140711-000801 abcd -rw-rw-r-- 1 test test 0 20140712-115101 abd -rw-rw-r-- 1 test test 6438 20140712-115330 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 20140712-115405 screenfetch-dev drwxrwxr-x 2 test test 4096 20140712-121208 test1 test@test-vm-ubuntu:~/test$
なお、表示形式の書き方は以下のようになっている。
- %Y…年(4桁)
- %y…年(2桁)
- %m…月
- %B…月(◯月)
- %d…日
- %a…曜日(1文字)
- %A…曜日(◯曜日)
- %H…時
- %M…分
- %S…秒
10.「-l」オプション実行時の「合計」を表示させない
「-l」オプション実行時に出力される「合計」を出力させない場合は、「tail」コマンドと組み合わせて実行する。
test@test-vm-ubuntu:~/test$ ls -l | tail -n +2 -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 6 7月 13 10:01 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$
11.最後に更新したファイルを表示させる
最後に更新したファイルのみ表示させる場合は、「-t」オプションと「head」コマンドを組み合わせて対応する。
test@test-vm-ubuntu:~/test$ ls -Alct|head -2 合計 152 -rw-rw-r-- 1 test test 0 7月 13 00:38 aaa.xls
最後に更新したファイル名だけを取得させたい場合は、以下のように実行する
test@test-vm-ubuntu:~/test$ ls -t | head -1 aaa.xls test@test-vm-ubuntu:~/test$
これを応用し、直接編集をする場合(例ではvimを用いる)には、以下のようなコマンドを実行する。
vim `ls -t | head -1`
この手法は、ファイルサイズやアクセス時間でも応用可能だ。
12.lsとwatchでファイルやディレクトリを監視する
lsを定期的に実行させてファイルの動向を監視するのであれば、「watch」コマンドと組み合わせる。
例えば、以下のようにコマンドを実行することで、1秒ごとに「ls -lAt | head -5」を実行させ監視することができる。
watch -n 1 "ls -lAt | head -5"
13.エイリアス設定でコマンド入力を短縮させる
lsコマンドで、よく使うオプションを含めてエイリアス設定を行う事も出来る。
デフォルトでは「ll(ls -alF相当)」が有名だろう。
例えば、サイズが最も大きいファイル3個のみを表示させ、1行目の「合計」を表示させない「ls -l | tail -n +2」を短縮した場合。
test@test-vm-ubuntu:~/test$ lt2 コマンド 'lt2' は見つかりませんでした。もしかして: コマンド 'lt' - パッケージ 'looptools' (universe) コマンド 'rt2' - パッケージ 'rttool' (universe) lt2: コマンドが見つかりません test@test-vm-ubuntu:~/test$ alias lt2="ls -l | tail -n +2" test@test-vm-ubuntu:~/test$ lt2 -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 6 7月 13 10:01 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$ ls -l | tail -n +2 -rw-rw-r-- 1 test test 0 7月 12 11:50 aaa -rw-rw-r-- 1 test test 6 7月 13 10:01 aaa.xls -rw-rw-r-- 1 test test 0 7月 12 12:16 aaa.xlsx lrwxrwxrwx 1 test test 17 7月 12 11:52 abc -> /home/test/timing -rw-rw-r-- 2 test test 1212 7月 11 00:08 abcd -rw-rw-r-- 1 test test 0 7月 12 11:51 abd -rw-rw-r-- 1 test test 6438 7月 12 11:53 archey-0.2.8.deb -rw-rw-r-- 1 test test 138155 7月 12 11:54 screenfetch-dev drwxrwxr-x 2 test test 4096 7月 12 12:12 test1 test@test-vm-ubuntu:~/test$
14.ファイルの有無確認
ファイルの有無確認でもlsコマンドは使える。
その際に意識しておきたいのがexitコードだ。lsコマンドでは、ファイルが存在する場合は0を、存在しない場合は2を返すようになっている。
test@test-vm-ubuntu:~/test$ ls aaa aaa.xls aaa.xlsx abc abcd abd archey-0.2.8.deb screenfetch-dev test1 test@test-vm-ubuntu:~/test$ ls aaa aaa test@test-vm-ubuntu:~/test$ echo $? 0 test@test-vm-ubuntu:~/test$ ls adadada ls: adadada にアクセスできません: そのようなファイルやディレクトリはありません test@test-vm-ubuntu:~/test$ echo $? 2 test@test-vm-ubuntu:~/test$
15.ファイル名と容量だけを取得する
「-l」オプションを実行する際、特定の値だけ欲しい場合は「awk」と組み合わせると良いだろう。
test@test-vm-ubuntu:~/test$ ls -lAh | awk '{ print $9 $5 }' | tail -n +2 .ass5 aaa0 aaa.xls6 aaa.xlsx0 abc17 abcd1.2K abd0 archey-0.2.8.deb6.3K screenfetch-dev135K test14.0K test@test-vm-ubuntu:~/test$
16.ディレクトリだけを表示させたい
ディレクトリだけを表示させたい場合は、「-d」オプションをもちいて「*/」となるものだけを表示させればいい。
test@test-vm-ubuntu:~/test$ ls -lAd */ drwxrwxr-x 2 test test 4096 7月 12 12:12 test1/ test@test-vm-ubuntu:~/test$
番外編 lsで出来ない事が出来るコマンド
番外編として、lsコマンドと似ているけど、lsでは出来ない事が出来るものを紹介する。
サブディレクトリごとの容量を合計する「du」コマンド
duコマンドは、サブディレクトリの容量を合計して表示してくれるコマンドだ。
以下は、あるフォルダの配下に有るサブディレクトリの合計容量が大きい順に5個表示させたもの。
※この時やっかいなのが、duにはソートオプションがないため、「-h」を用いると正常にソートさせることができない。
test@test-vm-ubuntu:~$ du | sort -nr | head -5 36244 . 18804 ./.cache 16636 ./.cache/mozilla 16632 ./.cache/mozilla/firefox 16628 ./.cache/mozilla/firefox/716h2o3r.default
フォルダツリーを作成する「tree」コマンド
サブディレクトリの中にあるファイルも確認し、フォルダツリーを作成することが出来るのが「tree」コマンドだ。
実行すると、以下のようなフォルダツリーが生成される。
lsコマンドは、きっと他にも色々な使い方があるかもしれない。
もしご存知の人がいたら教えて下さい。