GNU/Linux >> Linux の 問題 >  >> Linux

Linux での基本的な「ls」コマンドの例

はじめに

この記事では、Linux で Linux ls コマンドを使用する方法を学習します。基本的に、lsコマンドは、ファイルとディレクトリを一覧表示するために使用されるLinuxの基本的なコマンドです。 ls コマンドには非常に多くの引数と機能が付属しており、ファイルやディレクトリを日付順、サイズ順で並べ替えたり、隠しファイルやディレクトリ、アクセス許可、inode 情報などを確認したりできます。

では、便利で非常に重要な Linux ls コマンドを例とともに見てみましょう。

Linux ls コマンドの便利な引数:

  • l – ロング リスト形式でファイルとディレクトリを一覧表示する
  • – 非表示のファイルとディレクトリを確認する。
  • – Inode 情報を確認する。
  • いいえ – ファイルとディレクトリが属する所有者とグループの UID と GID 番号をリストします。
  • – それらが属するファイルとディレクトリのグループを一覧表示します。
  • G – ユーザーが属するファイルやディレクトリのグループではなく、ユーザーを一覧表示する
  • – ファイルとディレクトリのサイズを人間が読める形式で記載してください。

Linux ls コマンドと例

[root@localhost ~]# ls       ### List Files and Directories
anaconda-ks.cfg  Documents    install.log.syslog  Public
data             Downloads    Music               Templates
Desktop          install.log  Pictures            Videos

引数 -l を指定して ls コマンドを使用できます アクセス許可、所有者、グループ、サイズ、作成および変更された月、日時、ファイル名など、ファイルとディレクトリに関連するより詳細な情報を取得するには...

[root@localhost ~]# ls -l   # Long Listing of Files and Directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

特定のファイルの詳細を一覧表示するには、以下のコマンドを参照してください。

[root@localhost ~]# ls -l file.txt   # List a Particular File
-rw-r--r--. 1 root root 0 Apr 26 10:52 file.txt

特定のディレクトリの内容を一覧表示するには、以下のコマンドを参照してください。

[root@localhost ~]# ls -l /mydata/   # List a Particular Directory
total 0
-rw-r--r--. 1 root root 0 Apr 26 10:53 file1.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file2.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file3.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file4.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file5.txt

引数 -a を指定して Linux ls コマンドを使用すると、隠しファイルとディレクトリを確認できます。 .

[root@localhost ~]# ls -a   # Checking hidden files and directories
.                .config    .gconf           .gvfs               Pictures
..               .cshrc     .gconfd          .ICEauthority       Public
anaconda-ks.cfg  data       .gnome2          install.log         .pulse
.bash_history    .dbus      .gnome2_private  install.log.syslog  .pulse-cookie
.bash_logout     Desktop    .gnote           .local              .ssh
.bash_profile    Documents  .gnupg           .mozilla            .tcshrc
.bashrc          Downloads  .gstreamer-0.10  Music               Templates
.cache           .esd_auth  .gtk-bookmarks   .nautilus           Videos

ファイルとディレクトリの inode 数を確認するには、ls コマンドにオプション -i を指定して使用します。 .

[root@localhost ~]# ls -i  # Checking Inode numbers of files and directories
401494 anaconda-ks.cfg  785089 Downloads           785094 Pictures
917077 data             392451 install.log         785091 Public
785088 Desktop          392452 install.log.syslog  785090 Templates
785092 Documents        785093 Music               785095 Videos

または -i を使用できます -l を使用した引数 以下のコマンドに示すように。

[root@localhost ~]# ls -li
total 100
401494 -rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
917077 drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
785088 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
785092 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
785089 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
392451 -rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
392452 -rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
785093 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
785094 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
785091 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
785090 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
785095 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

引数 -o を指定した ls コマンド ファイルとディレクトリの所有者を表示し、グループは表示しません。

[root@localhost ~]# ls -o   # Listing only owner of files and directories
total 100
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

オプション -m を指定した Linux ls コマンド コンマで区切られたファイルとディレクトリを一覧表示します。

[root@localhost /]# ls -m   # List seperated by comma
bin, boot, dev, etc, home, lib, lib64, lost+found, media, mnt, mydata, opt,
proc, root, sbin, selinux, srv, sys, tmp, users, usr, var

オプション -Q を指定した ls コマンド ファイルとディレクトリの名前が引用符で囲まれているファイルとディレクトリを一覧表示します。以下の出力例を参照してください。

[root@localhost /]# ls -lQ   # Labels of Files and directories enclosed by Quotes
total 106
dr-xr-xr-x.   2 root root  4096 Apr 16 09:59 "bin"
dr-xr-xr-x.   5 root root  1024 Apr  9 02:45 "boot"
drwxr-xr-x.  19 root root  4120 Apr 26 08:51 "dev"
drwxr-xr-x. 102 root root 12288 Apr 26 10:34 "etc"
drwxr-xr-x.   9 root root  4096 Apr 20 09:39 "home"
dr-xr-xr-x.  10 root root  4096 Apr  9 02:39 "lib"
dr-xr-xr-x.   9 root root 12288 Apr 16 09:59 "lib64"
drwx------.   2 root root 16384 Apr  9 02:35 "lost+found"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "media"
drwxr-xr-x.   3 root root  4096 Apr  9 02:46 "mnt"
drwxr-xr-x.   2 root root  4096 Apr 26 10:53 "mydata"
drwxr-xr-x.   3 root root  4096 Apr  8 21:17 "opt"
dr-xr-xr-x. 162 root root     0 Apr 26 08:49 "proc"
dr-xr-x---.  27 root root  4096 Apr 26 10:52 "root"
dr-xr-xr-x.   2 root root 12288 Apr 16 09:59 "sbin"
drwxr-xr-x.   7 root root     0 Apr 26 08:49 "selinux"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "srv"
drwxr-xr-x.  13 root root     0 Apr 26 08:49 "sys"
drwxrwxrwt.  15 root root  4096 Apr 26 10:56 "tmp"
drwxrwxrwx.   3 root root  4096 Apr 20 09:22 "users"
drwxr-xr-x.  13 root root  4096 Apr  9 02:35 "usr"
drwxr-xr-x.  22 root root  4096 Apr 16 09:24 "var"

引数 -R を指定した Linux ls コマンドを使用して、ディレクトリを再帰的に一覧表示できます。 .再帰的には、ツリー形式ですべてのサブディレクトリを含むすべてのディレクトリを一覧表示することを意味します。

[root@localhost ~]# ls -R   # Listing Directories Recursively
.:
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

./data:
a.txt  b.txt  c.txt

./Desktop:
data1  data2  data3

./Desktop/data1:

./Desktop/data2:

./Desktop/data3:

./Documents:

./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:

~ を使用した ls コマンド ホーム ディレクトリに切り替えます。

[root@localhost Downloads]# pwd 
/root/Downloads
[root@localhost Downloads]# ls ~   # Switch to Home Directory
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

-t を指定した Linux ls コマンド ファイルとディレクトリを変更日順に昇順で一覧表示します。つまり、変更日が大きい方が最初になります。

[root@localhost ~]# ls -lt   # Sort by Modified by date
total 100
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates

引数 -S を指定した ls コマンドを使用して、ファイルとディレクトリをサイズで並べ替えることができます。

[root@localhost ~]# ls -lS   # Sort by Size in ascending order
total 100
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt

オプション -h を指定した ls コマンドは、ファイルとディレクトリのサイズを人間が読める形式 (例:20K (20 KB)、15M (15 MB)) で一覧表示します。

[root@localhost ~]# ls -lh   # List the size in Human Readable format
total 100K
-rw-------. 1 root root 2.7K Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Apr 24 09:10 data
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Documents
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Downloads
-rw-r--r--. 1 root root    0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root  41K Apr  9 02:44 install.log
-rw-r--r--. 1 root root 9.0K Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Music
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Public
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Templates
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Videos

オプション -n を指定した ls コマンドは、ファイルとディレクトリの UID (ユーザー ID) と GID (グループ ID) 番号を表示します。

[root@localhost ~]# ls -ln   # List the UID and GID Numbers
total 108
-rw-------. 1 0   0  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 0   0  4096 Apr 24 09:10 data
drwxr-xr-x. 5 0   0  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 0   0     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 0   0 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 0   0  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 0 501  4096 Apr 28 09:48 test
drwxr-xr-x. 2 0 506  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Videos

グループではなく、ファイルとディレクトリの所有者のみを一覧表示する場合は、引数 -G を指定して Linux ls コマンドを使用します。 .

[root@localhost ~]# ls -lG   # Disable the Group Listing and only lists owners
total 108
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 5 root  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr 28 09:48 test
drwxr-xr-x. 2 root  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

-g を指定した ls コマンド それらが属するファイルとディレクトリのグループを一覧表示します。

[root@localhost ~]# ls -g   # Lists groups to which they belongs
total 108
-rw-------. 1 root        2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root        4096 Apr 24 09:10 data
drwxr-xr-x. 5 root        4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root           0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root       41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root        9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 sales       4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Videos

-F を指定した ls コマンド 末尾に / を付けてディレクトリを一覧表示します。以下の出力例を参照してください。

[root@localhost ~]# ls -lF   # Add's / at the end of the directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads/
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos/

Linux ls 関連のコマンドとその引数の詳細については、Linux システムで以下のコマンドを実行できます。

[root@localhost ~]# man help
[root@localhost ~]# ls --help


Linux
  1. Linux での sa コマンドの例

  2. Linux での ac コマンドの例

  3. Linux での df コマンドの例

  1. 7 Linux df コマンドの例

  2. 8 Linux TR コマンドの例

  3. Linux での du コマンドの例

  1. Linux での基本的な「chmod」コマンドの例

  2. Linux での rm コマンドの例

  3. Linux での ps コマンドの例