ls

提供: FreeBSD入門
2014年8月16日 (土) 15:00時点におけるDaemon (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索
スポンサーリンク

ls とは、ディレクトリのコンテンツをリスト表示するコマンドです。lsコマンドで、知っておくと得するオプションや使い方を紹介します。lsコマンドは、コマンド単体で、ファイルの変更時間やファイルのサイズでソートする機能を持っています。

読み方

ls
えすえす

概要

lsコマンドは、Unix を触る時に、おそらく、最初に学ぶコマンド群に入ります。

意外とlsコマンド単体で、いろいろな機能があるので、知っておいて損はありません。

使い方

ドットファイル(隠しファイル)を表示する

lsは、デフォルトでは、ドット(.)ではじまるファイル(ドットファイル)を表示しません。-a をつけることで表示できます。

$ ls /root
tmp/
$ ls -a /root
./           .distlib/    .k5login     .npm/        .rnd         .vimrc
../          .history     .lesshst     .pip/        .subversion/ tmp/
.cshrc       .john/       .login       .profile     .viminfo

そのディレクトリ(.)と親ディレクトリ(..)を省略するには、-Aを使います。

$ ls -A /root
.cshrc       .john/       .login       .profile     .viminfo
.distlib/    .k5login     .npm/        .rnd         .vimrc
.history     .lesshst     .pip/        .subversion/ tmp/

ディレクトリに/をつける

$ ls -F /usr/src
COPYRIGHT          UPDATING           gnu/               sbin/
LOCKS              bin/               include/           secure/
MAINTAINERS        cddl/              kerberos5/         share/
Makefile           contrib/           lib/               sys/
Makefile.inc1      crypto/            libexec/           tools/
ObsoleteFiles.inc  etc/               release/           usr.bin/
README             games/             rescue/            usr.sbin/

再帰的にディレクトリをlsする

サブディレクトリを再帰的にlsするには、-Rオプションを使います。

$ ls -R /usr/local/etc/apache24
Includes/   envvars.d/  httpd.conf  mime.types  rand.txt    server.csr
Makefile    extra/      magic       modules.d/  server.crt  server.key
 
/usr/local/etc/apache24/Includes:
kaworu.conf       no-accf.conf      sec_rewrite.conf
 
/usr/local/etc/apache24/envvars.d:
 
/usr/local/etc/apache24/extra:
httpd-autoindex.conf           httpd-mpm.conf
httpd-dav.conf                 httpd-multilang-errordoc.conf
httpd-default.conf             httpd-ssl.conf
httpd-info.conf                httpd-userdir.conf
httpd-languages.conf           httpd-vhosts.conf
httpd-manual.conf              proxy-html.conf
 
/usr/local/etc/apache24/modules.d:
README_modules.d

ファイルサイズをわかりやすくする

-lオプションでファイルサイズを調べることができますが、ファイルサイズが大きいとバイト単位の表示では、読むのが大変になります。よりヒューマンリーダビリティを向上するには、KB, MB, GB と表示することです。-hオプションを使用すると適当に単位をつけて表示してくれます。

$ ls -Slh /var/log |head
total 556
-rw-r--r--  1 root  wheel      93K  223 21:28 bsdinstall_log
-rw-------  1 root  wheel      63K  816 04:01 cron
-rw-------  1 root  wheel      45K  816 03:39 auth.log
-rw-r--r--  1 root  wheel      43K  816 03:55 httpd-access.log
-rw-r--r--  1 root  wheel      42K  816 03:54 messages
-rw-r--r--  1 root  wheel      21K  816 03:47 utx.log
-rw-r--r--  1 root  wheel      13K  329 22:00 messages.2.bz2
-rw-r--r--  1 root  wheel      11K  717 02:00 messages.0.bz2
-rw-r--r--  1 root  wheel      11K  522 17:00 messages.1.bz2

行ごとのエントリ数を1つにする

詳細表示しないときにlsは、1行に複数のファイルを表示します。

$ ls /etc/ssh
moduli                  ssh_host_ecdsa_key      ssh_host_rsa_key
ssh_config              ssh_host_ecdsa_key.pub  ssh_host_rsa_key.pub
ssh_host_dsa_key        ssh_host_key            sshd_config
ssh_host_dsa_key.pub    ssh_host_key.pub

-1オプションで1つのエントリにできます。

$ ls -1 /etc/ssh
moduli
ssh_config
ssh_host_dsa_key
ssh_host_dsa_key.pub
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
ssh_host_key
ssh_host_key.pub
ssh_host_rsa_key
ssh_host_rsa_key.pub
sshd_config

変更時間でソートする

新しく作成したファイルを探すときに、-t が便利です。

$ ls -lt .vim/bundle/neobundle.vim
total 44
-rw-r--r--  1 foo  user  4123  420 10:35 README.md
drwxr-xr-x  4 foo  user   512  420 10:35 autoload/
drwxr-xr-x  2 foo  user   512  420 10:35 bin/
drwxr-xr-x  2 foo  user   512  420 10:35 doc/
drwxr-xr-x  2 foo  user   512  420 10:35 test/
drwxr-xr-x  2 foo  user   512  46 00:36 plugin/
drwxr-xr-x  2 foo  user   512  46 00:36 syntax/
-rw-r--r--  1 foo  user  1068  224 08:34 LICENSE-MIT.txt
drwxr-xr-x  2 foo  user   512  224 08:34 vest/
drwxr-xr-x  2 foo  user   512  22  2013 ftdetect/

-r (リバース)をつけて、逆にソートできます。

$ ls -rlt .vim/bundle/neobundle.vim
total 44
drwxr-xr-x  2 kaworu  user   512  22  2013 ftdetect/
drwxr-xr-x  2 kaworu  user   512  224 08:34 vest/
-rw-r--r--  1 kaworu  user  1068  224 08:34 LICENSE-MIT.txt
drwxr-xr-x  2 kaworu  user   512  46 00:36 syntax/
drwxr-xr-x  2 kaworu  user   512  46 00:36 plugin/
drwxr-xr-x  2 kaworu  user   512  420 10:35 test/
drwxr-xr-x  2 kaworu  user   512  420 10:35 doc/
drwxr-xr-x  2 kaworu  user   512  420 10:35 bin/
drwxr-xr-x  4 kaworu  user   512  420 10:35 autoload/
-rw-r--r--  1 kaworu  user  4123  420 10:35 README.md

ファイルサイズでソートする

ストレージの空き容量が逼迫してきた、パーティションがあふれた!といったときに、大きいファイルを探して消したい、というときがあります。

$ ls -lS /etc/ssh
total 280
-rw-r--r--  1 root  wheel  242153  117  2014 moduli
-rw-r--r--  1 root  wheel    3969  117  2014 sshd_config
-rw-r--r--  1 root  wheel    1724  117  2014 ssh_config
-rw-------  1 root  wheel    1679  223 12:29 ssh_host_rsa_key
-rw-------  1 root  wheel     978  223 12:29 ssh_host_key
-rw-------  1 root  wheel     672  223 12:29 ssh_host_dsa_key
-rw-r--r--  1 root  wheel     643  223 12:29 ssh_host_key.pub
-rw-r--r--  1 root  wheel     603  223 12:29 ssh_host_dsa_key.pub
-rw-r--r--  1 root  wheel     395  223 12:29 ssh_host_rsa_key.pub
-rw-------  1 root  wheel     227  223 12:29 ssh_host_ecdsa_key
-rw-r--r--  1 root  wheel     175  223 12:29 ssh_host_ecdsa_key.pub

もちろん-rで逆向きにできます。

$ ls -rlS /etc/ssh
total 280
-rw-r--r--  1 root  wheel     175  223 12:29 ssh_host_ecdsa_key.pub
-rw-------  1 root  wheel     227  223 12:29 ssh_host_ecdsa_key
-rw-r--r--  1 root  wheel     395  223 12:29 ssh_host_rsa_key.pub
-rw-r--r--  1 root  wheel     603  223 12:29 ssh_host_dsa_key.pub
-rw-r--r--  1 root  wheel     643  223 12:29 ssh_host_key.pub
-rw-------  1 root  wheel     672  223 12:29 ssh_host_dsa_key
-rw-------  1 root  wheel     978  223 12:29 ssh_host_key
-rw-------  1 root  wheel    1679  223 12:29 ssh_host_rsa_key
-rw-r--r--  1 root  wheel    1724  117  2014 ssh_config
-rw-r--r--  1 root  wheel    3969  117  2014 sshd_config
-rw-r--r--  1 root  wheel  242153  117  2014 moduli

top風にディレクトリを監視する

top風に特定のディレクトリの状況を ls でモニタリングしつづけたい、というニーズには、シェルの機能を使って頑張るか、FreeBSDならtoplessを使う方法で実現できます。 Linuxでは、watchコマンドで実現しますが、FreeBSDのwatchコマンドは、機能が違うので、代わりにtoplessを使います。

toplessは、ベースシステムのコマンドではないので、別途インストールします。 pkgコマンドでインストールする場合

sudo pkg install topless
$ topless /bin/ls /path/to

topless は、ps や netstat などのコマンドと併用すると面白いでしょう。

コマンドラインでがんばるなら、以下のようになります。 csh系では使えません。zshを使用してます(shでも可)。

while true; do ls -lt /var/log|head ; sleep 2; done

関連項目




スポンサーリンク