地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

【Linux】lsコマンド

いまさらですが、lsコマンドについてまとめます。

lsコマンド

list segmentsの略。 ファイルやディレクトリの情報を表示する。

$ ls
a.txt  b.txt  c.txt  test  test2

上記コマンドでカレントディレクトリ内のファイルの一覧を表示する。

ls -a : すべてのファイルやディレクトリを表示する

$ ls -a 
.  ..  a.txt  b.txt  c.txt  test  test2

-aオプションをつけることで、ピリオド始まりのファイルやフォルダも表示される。

-Aオプションを付けると、カレントディレクトリ(.)と親ディレクトリ(..)以外のピリオド始まりのファイルやフォルダも表示される。

ls -l:ファイルの詳細を表示する

$ ls -l
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt
drwxr-xr-x 2 root root 4096 Dec 23 13:14 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
項目 説明
-rw-r--r-- ファイルの種類(一番左の一文字) + パーミッション
1 ハードリンクの数
root root 所有者(左)と所有グループ(右)
12030 ファイルサイズ
Oct 6 19:15 タイムスタンプ
test.txt ファイル名

ファイルの種類として通常使われるのは以下の3つです。 - : ファイル d : ディレクトリ l : シンボリックリンク

ls -F:名前の後ろにファイル識別子を付ける

$ ls -F
a.txt  b.txt  c.txt  test/  test2/ 

ファイル識別子は以下のようになる。 / : ディレクトリ @ : シンボリックリンク * : 実行可能形式ファイル(実行可能パーミッションがついているもの)

ls -d:ディレクトリに含まれているものの情報ではなく、ディレクトリ自体の情報を表示する

$ ls -ld
drwxr-xr-x 4 root root 4096 Dec 23 13:15 .

ls -i:iノード番号も含んだ情報を表示する

$ ls -li
14724 -rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
14720 -rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
14722 -rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt
 6103 drwxr-xr-x 2 root root 4096 Dec 23 13:14 test
14721 drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2

ls -1:情報を1行に1件表示する

$ ls -1
a.txt
b.txt
c.txt
test
test2

通常は横1列に表示されますが、縦に1つずつ表示されます。

ls -r:逆順に表示する

$ ls
a.txt  b.txt  c.txt  test  test2
$ ls -r
test2  test  c.txt  b.txt  a.txt

ls -R:再帰的にディレクトリの中身も表示する

$ ls -R
.:
a.txt  b.txt  c.txt  test  test2

./test:
test.txt  test1.txt

./test2:

ls -t:更新時間順に情報を表示する

$ ls -lt
drwxr-xr-x 2 root root 4096 Dec 23 13:24 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt

ls -S:ファイルサイズ順に情報を表示する

$ ls -lS
drwxr-xr-x 2 root root 4096 Dec 23 13:24 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt

ls -X:拡張子のアルファベット順に情報を表示する

$ ls -l
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt
drwxr-xr-x 2 root root 4096 Dec 23 13:24 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
$ ls -lX
drwxr-xr-x 2 root root 4096 Dec 23 13:24 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt

ls -x:縦方向ではなく横方向に情報を並べる

$ ls
a.txt  test
b.txt  test2
c.txt
$ ls -x
a.txt  b.txt
c.txt  test
test2

ls -m:情報をカンマ区切りで表示する

$ ls -m
a.txt, b.txt, c.txt, test, test2

ls -Q:情報をダブルクォートで囲んで表示する

$ ls -Q
"a.txt"  "b.txt"  "c.txt"  "test"  "test2"

ls -h:ファイルサイズの形式をわかりやすい単位で表示する

$ ls -l
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt
drwxr-xr-x 2 root root 4096 Dec 23 13:24 test
drwxr-xr-x 2 root root 4096 Dec 23 13:15 test2
$ ls -lh
-rw-r--r-- 1 root root   11 Dec 23 13:11 a.txt
-rw-r--r-- 1 root root   36 Dec 23 13:12 b.txt
-rw-r--r-- 1 root root    0 Dec 23 13:07 c.txt
drwxr-xr-x 2 root root 4.0K Dec 23 13:24 test
drwxr-xr-x 2 root root 4.0K Dec 23 13:15 test2

ls --ignore=PATTERN:PATTERNに一致する情報以外の情報を表示する

$ ls
a.txt  b.txt  c.txt  test  test2
$ ls --ignore=*.txt
test  test2