「ls」の版間の差分

提供: FreeBSD入門
移動: 案内検索
(ページの作成:「ls とは、ディレクトリのコンテンツをリスト表示するコマンドです。 '''読み方''' ;ls:えすえす __TOC__ == 概要 == lsコ...」)
 
行1: 行1:
[[ls]] とは、ディレクトリのコンテンツをリスト表示するコマンドです。
+
[[ls]] とは、ディレクトリのコンテンツをリスト表示するコマンドです。[[ls]]コマンドで、知っておくと得するオプションや使い方を紹介します。
  
 
'''読み方'''
 
'''読み方'''

2014年8月16日 (土) 13:33時点における版

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

行ごとのエントリ数を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

関連項目