Linuxのよく使うコマンド(lsで一覧表示)

目次

表示項目

ファイルの一覧を詳細で表示(ls -l)

$ ls -l
-rw-rw-r-- 1 test test  0  7月 20 08:34 test1.txt
-rw-rw-r-- 1 test test  0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test 23  7月 20 08:52 testdir1

オプションのlはファイルの一覧を詳細で表示します。

 

UIDとGIDを表示(ls -n)

$ ls -n
-rw-rw-r-- 1 1000 1000  0  7月 20 08:34 test1.txt
-rw-rw-r-- 1 1000 1000  0  7月 21 08:26 test2.txt
drwxrwxr-x 2 1000 1000 23  7月 20 08:52 testdir1

オプションのnは、所有者とグループの代わりにUIDとGIDが表示されます。

 

最終アクセス日時を表示(ls -lu)

$ ls -lu
-rw-rw-r-- 1 test test 9672  7月 21 08:51 test1.txt
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test   23  7月 20 09:20 testdir1
$ ls -l
-rw-rw-r-- 1 test test 9672  7月 21 08:41 test1.txt
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1

オプションのuは表示する時刻をアクセス時刻(atime)に切り替えます。

ls -luは、uをつけたので最終アクセス日時が表示されています。
ls -lは、uをつけていないので更新日時が表示されています。

 

ファイルまたはディレクトリを1行に1件ずつ表示(ls -1)

$ ls -1
test1.txt
test2.txt
testdir1

ファイルまたはディレクトリを1行に1件ずつ表示します。

パイプでほかのコマンドに渡すときに便利です。

 

隠しファイルと隠しディレクトリを表示(ls -la)

$ ls -la
drwxrwxr-x.  4 test test   91  7月 21 08:41 .
drwx------. 22 test test 4096  7月 21 08:41 ..
-rw-rw-r--   1 test test    0  7月 20 08:35 .test2.txt
drwxrwxr-x   2 test test    6  7月 20 08:36 .testdir2
-rw-rw-r--   1 test test 9672  7月 21 08:41 test1.txt
-rw-rw-r--   1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x   2 test test   23  7月 20 08:52 testdir1

オプションのaは隠しファイルと隠しディレクトリを表示します。

5行目は、隠しファイルです。
6行目は、隠しディレクトリです。

 

.と..は除外で隠しファイルと隠しディレクトリを表示(ls -lA)

$ ls -lA
-rw-rw-r--   1 test test    0  7月 20 08:35 .test2.txt
drwxrwxr-x   2 test test    6  7月 20 08:36 .testdir2
-rw-rw-r--   1 test test 9672  7月 21 08:41 test1.txt
-rw-rw-r--   1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x   2 test test   23  7月 20 08:52 testdir1

 

ディレクトリだけ表示(ls -d */)

$ ls -d */
testdir1/

カレントディレクトリ直下が対象です。
隠しディレクトリは表示されません。

以下は、隠しディレクトリを表示します。

$ ls -d .*/

並び順

更新日の新しい順に表示する(ls -lt)

$ ls -lt
-rw-rw-r-- 1 test test 9672  7月 21 08:41 test1.txt
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1

オプションのtは更新日の新しい順に表示します。tはtimeのtです。

 

並び順を逆にする(ls -ltr)

$ ls -ltr
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
-rw-rw-r-- 1 test test 9672  7月 21 08:41 test1.txt

オプションのrは並び順を逆にします。rはreverseのrです。

tは更新日の新しい順にしてrでその逆順にしています。
→ファイルの古い順に表示します。

 

ファイルサイズの大きい順に表示(ls -lS)

$ ls -lS
-rw-rw-r-- 1 test test 9672  7月 21 08:41 test1.txt
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt

オプションのSはファイルサイズの大きい順に表示します。

 

ソートせずに結果を表示(ls -U)

Uはソートしないため高速で表示されます。
主にファイルが大量にあるときに使用します。

 

以下は先頭の10行のみ表示します。

$ ls -l `ls -U | head`

ls -U | headの出力結果をファイル名としてls -lに渡しています。

ステップ1: バッククォートの中が先に実行される
ステップ2: その出力結果がそのまま外側のコマンドに埋め込まれる

 

以下でも動作は同じです。

$ ls -l $(ls -U | head)

バッククォートは古い書き方で、現在は$()を使うのが推奨されています。

 

以下は1ページ単位で表示しスクロールしません。エンターで下に移動します。
ctrl + cで終了します。

$ ls -U | more 

件数のカウント

ファイルとディレクトリの合計数を数える(ls -1U | wc -l)

$ ls -1U | wc -l
3

lsコマンドとwcコマンドを使用します。

ファイルのみをカウントしたい場合は以下にします。

$ find . -maxdepth 1 -type f | wc -l

 

表示の補助

サイズを読みやすい形式にする(ls -lh)

$ ls -lh
-rw-rw-r-- 1 test test 9.5K  7月 21 08:41 test1.txt
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1
$ ls -l
-rw-rw-r-- 1 test test 9672  7月 21 08:41 test1.txt
-rw-rw-r-- 1 test test    0  7月 21 08:26 test2.txt
drwxrwxr-x 2 test test   23  7月 20 08:52 testdir1

オプションのhはサイズを読みやすい形式にします。

1行目は、hをつけたので9.5Kと表示されています。
6行目は、hをつけていないので9672と表示されています。

 

カンマ区切り(csv形式)で表示する(ls -m)

$ ls -m
test1.txt, test2.txt, testdir1

オプションのmはカンマ区切り(csv形式)で表示します。

 

ファイル・ディレクトリをダブルコーテーションで括る(ls -lQ)

$ ls -lQ
-rw-rw-r-- 1 test test 9672  7月 21 08:41 "test1.txt"
-rw-rw-r-- 1 test test    0  7月 21 08:26 "test2.txt"
drwxrwxr-x 2 test test   23  7月 20 08:52 "testdir1"

オプションのQはファイル・ディレクトリをダブルコーテーションで括ります。

関連の記事

Linuxのよく使うコマンド(cp / rm / mv / mkdir)
Linux ディスク容量・ファイルシステム確認コマンド

△上に戻る