コマンドを探すコマンドwhatisとaproposを使いこなす

LinuxのCLI環境において、操作に欠かせないコマンド。

使用頻度が高いlsやcdといった主要なコマンドは自然と体に染み付いてしまいますが、Linuxの世界には大量のコマンドが存在し、それらを全て覚えるのは当然の如く至難のわざです。

一方では、業務や実務の内容によっては、一度も使うことが無い、使う必要がないコマンドもあるでしょう。しかしながら、意外に知らないと損するコマンドもあったりするかもしれません。

そんな時に便利な、コマンドを探すためのコマンド、whatisaproposをご紹介します。

もしかして、こんなことをしてくれるコマンドってあるのかな?といった疑問を感じた時や、ウロ覚えの文字数の多いコマンドがわからなくなった時など、是非これらのコマンドを使ってみてください。

whatisコマンド

whatisは、コマンド名称を引数にして入力することで、そのコマンドの内容を要約表示してくれるコマンドです。

$ whatis [オプション] キーワード

例えば、キーワードを「ls」や「pwd」で実行してみると以下のような結果が表示されます。

$ whatis ls
ls (1)               - ディレクトリの内容をリスト表示する
ls (1p)              - list directory contents

$ whatis pwd
pwd (1)              - 現在の作業ディレクトリの名前を出力する
pwd (1p)             - return working directory name

whatis自体を引数にして実行してみましょう。

$ whatis whatis
whatis (1) - マニュアルページの要約文を表示する

whatisコマンドの内容を要約表示しました。

コマンド名がうろ覚えの場合は、-wオプションを付けて、引数にワイルドカードを使います。引数にはシェル展開を防ぐために引用符を付けます。

下の例では、sshで始まるコマンドを探してくれています。

$ whatis -w "ssh*"
ssh (1)              - OpenSSH SSH client (remote login program)
ssh-add (1)          - adds private key identities to the authentication agent
ssh-agent (1)        - authentication agent
ssh-copy-id (1)      - use locally available keys to authorise logins on a remote machine
ssh-keygen (1)       - authentication key generation, management and conversion
ssh-keyscan (1)      - gather ssh public keys
ssh-keysign (8)      - ssh helper program for host-based authentication
ssh-pkcs11-helper (8) - ssh-agent helper program for PKCS#11 support
ssh_config (5)       - OpenSSH SSH client configuration files
sshd (8)             - OpenSSH SSH daemon
sshd_config (5)      - OpenSSH SSH daemon configuration file

aproposコマンド

aproposはwhatis同様の動きをするコマンドです。引数に設定されたキーワードをコマンド名称とマニュアルの要約を対象に検索して表示してくれます。

$ apropos [オプション] キーワード

whatis同様、「ls」と「pwd」をキーワードにしてオプションなしで実行してみましょう。

$ apropos ls
_llseek (2)          - ファイルの読み書きオフセットの位置を変える
backtrace_symbols (3) - アプリケーション自身でのデバッグのサポート
backtrace_symbols_fd (3) - アプリケーション自身でのデバッグのサポート
credentials (7)      - 認証に用いられるプロセスの識別子
dircolors (1)        - ls 用の色設定
dlsym (3)            - 動的リンクを行うローダへの プログラミングインターフェース
false (1)            - 何もせず、失敗で終了する
get_thread_area (2)  - スレッド局所記憶 (TLS) 領域を取り出す
llseek (2)           - ファイルの読み書きオフセットの位置を変える
ls (1)               - ディレクトリの内容をリスト表示する
lsattr (1)           - Linux 第 2 拡張ファイルシステム (ext2fs) 上にあるファイルの 属性 (attribute) を表示する
lsdev (8)            - 組み込まれているハードウェアに関する情報を表示する
(中略)
XkbSetXlibControls (3) - Changes the state of the Library Controls
xlsatoms (1)         - list interned atoms defined on server
xlsclients (1)       - list client applications running on a display
xlsfonts (1)         - server font list displayer for X
xmlsec1 (1)          - sign, verify, encrypt and decrypt XML documents
xprt_register (3)    - library routines for remote procedure calls
xprt_unregister (3)  - library routines for remote procedure calls
XSetFillStyle (3)    - GC convenience routines
XSetWMProtocols (3)  - set or read a window's WM_PROTOCOLS property
yum-utils (1)        - tools for manipulating repositories and extended package management
zipdetails (1)       - display the internal structure of zip files

$ apropos pwd
lckpwdf (3)          - 暗号化されたパスワードファイル用ルーチン
pwd (1)              - 現在の作業ディレクトリの名前を出力する
pwdx (1)             - プロセスのカレントワーキングディレクトリを表示する
ulckpwdf (3)         - 暗号化されたパスワードファイル用ルーチン
pwd (1p)             - return working directory name
pwd.h (0p)           - password structure
unix_chkpwd (8)      - Helper binary that verifies the password of the current user

コメント名称またはマニュアルの要約に「ls」や「pwd」という文字列を含んだコマンドが大量に表示されました。aproposは標準ではキーワードを正規表現として扱います。

オプションにワイルドカードを意味する「-w」を指定してキーワードを指定すると、キーワードに完全一致するコマンドの内容を要約して表示してくれます。

$ apropos -w ls
dircolors (1)        - ls 用の色設定
ls (1)               - ディレクトリの内容をリスト表示する
ls (1p)              - list directory contents

$ apropos -w pwd
pwd (1)              - 現在の作業ディレクトリの名前を出力する
pwd (1p)             - return working directory name

$ apropos -w whatis
whatis (1)           - マニュアルページの要約文を表示する

この結果を見て、何か気がつきませんか?

先にご紹介したwhatisコマンドのオプションなしの実行結果とほぼ同じです。

$ whatis ls
ls (1)               - ディレクトリの内容をリスト表示する
ls (1p)              - list directory contents

$ whatis pwd
pwd (1)              - 現在の作業ディレクトリの名前を出力する
pwd (1p)             - return working directory name

$ whatis whatis
whatis (1) - マニュアルページの要約文を表示する

ちなみに、結果こそほぼ同じですが、whatisはコマンド名称だけを見ているのに対して、aproposはコマンド名称とマニュアルの要約両方を見ている点が異なります。

このことは、日本語マニュアルがインストールされている環境であれば、日本語のキーワードからもコマンド探しができることを意味しています。

$ apropos 仮想
console (4)          - コンソール端末 (console terminal) と仮想コンソール (virtual console)
console_ioctl (4)    - コンソール端末と仮想コンソールの ioctl
mremap (2)           - 仮想メモリ・アドレスを再マッピングする
vcs (4)              - 仮想コンソールメモリ (virtual console memory)
vcsa (4)             - 仮想コンソールメモリ (virtual console memory)
vhangup (2)          - 現在の端末を仮想的に (virtualy) ハングアップ (hangup) させる
vm86 (2)             - 仮想 8086 モードへ移行する
vm86old (2)          - 仮想 8086 モードへ移行する
vmstat (8)           - 仮想メモリの統計を報告する

$ apropos 検索
apropos (1)          - マニュアルページの名前と要約文を検索する
bsearch (3)          - ソートされた配列を二分木検索 (binary search) する
bzgrep (1)           - bzip2 圧縮されている可能性のあるファイルで、正規表現の検索をする
find (1)             - ディレクトリ階層をたどって、条件を満たすファイルを検索する
gawk (1)             - パターン検索・処理言語
host (1)             - ドメインサーバを使ってホスト名の検索を行なう
lfind (3)            - 配列を線形検索する
lkbib (1)            - 文献目録データベースを検索する
lookbib (1)          - 文献目録データベースを検索する
lsearch (3)          - 配列を線形検索する
manpath (1)          - マニュアルページの検索パスを設定します
wcschr (3)           - ワイド文字文字列中のワイド文字を検索する
wcscspn (3)          - ワイド文字文字列から、与えた文字集合に含まれる文字を検索する
wcspbrk (3)          - ワイド文字列から、与えられたワイド文字集合に含まれる文字を検索する
zgrep (1)            - 圧縮されている可能性のあるファイルで、正規表現の検索をする

whatisとaproposの違い

以下の例で、2つのコマンドでの検索結果の微妙な違いが確認できます。

$ apropos les*
(あまりにも大量に表示されるので省略します)

$ apropos -w les*
less (1)             - more の反対 (more のようなページャー)
lesskey (1)          - less のキー割り当てを指定する
ceil (3)             - ceiling function: smallest integral value not less than argument
ceilf (3)            - ceiling function: smallest integral value not less than argument
ceill (3)            - ceiling function: smallest integral value not less than argument
gvfs-less (1)        - Execute less on the output of gvfs-cat
isless (3p)          - test if x is less than y
islessequal (3p)     - test if x is less than or equal to y
islessgreater (3p)   - test if x is less than or greater than y
less (3pm)           - perl pragma to request less of something
lessecho (1)         - expand metacharacters

$ whatis les*
les*: 適切なものはありませんでした。

$ whatis -w les*
less (1)             - more の反対 (more のようなページャー)
lesskey (1)          - less のキー割り当てを指定する
less (3pm)           - perl pragma to request less of something
lessecho (1)         - expand metacharacters
$ apropos passwd
afppasswd (1)        - netatalk パスワード管理ユーティリティ
chpasswd (8)         - パスワードファイルをバッチ処理で更新する
gpasswd (1)          - /etc/groupファイルを管理する
passwd (1)           - ユーザパスワードを変更する
passwd (5)           - パスワードファイル
passwd2des (3)       - RFS パスワード暗号化
pwupdate (8)         - NIS マップ passwd および shadow を更新する
rpc.yppasswdd (8)    - NIS パスワード更新デーモン
yppasswd (1)         - NIS データベースのパスワードを変更する
yppasswdd (8)        - NIS パスワード更新デーモン
chgpasswd (8)        - update group passwords in batch mode
fgetpwent_r (3)      - get passwd file entry reentrantly
getpwent_r (3)       - get passwd file entry reentrantly
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1)          - Change group or user password
lppasswd (1)         - add, change, or delete digest passwords.
pam_localuser (8)    - require users to be listed in /etc/passwd
sslpasswd (1ssl)     - compute password hashes
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
saslpasswd2 (8)      - set a user's sasl password
smbpasswd (5)        - The Samba encrypted password file
vncpasswd (1)        - change the VNC password

$ apropos -w passwd
passwd (1)           - ユーザパスワードを変更する
passwd (5)           - パスワードファイル
pwupdate (8)         - NIS マップ passwd および shadow を更新する
fgetpwent_r (3)      - get passwd file entry reentrantly
getpwent_r (3)       - get passwd file entry reentrantly
sslpasswd (1ssl)     - compute password hashes
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd

$ whatis passwd
passwd (1)           - ユーザパスワードを変更する
passwd (5)           - パスワードファイル
sslpasswd (1ssl)     - compute password hashes

$ whatis -w passwd
passwd (1)           - ユーザパスワードを変更する
passwd (5)           - パスワードファイル
sslpasswd (1ssl)     - compute password hashes

まとめ

調べたいコマンドの名称がはっきりとわかっている場合はオプションなしのwhatis、名称がうろ覚えの時はワイルドカード設定ができるwhatis -wがおすすめ。

aproposはキーワードに関連したコマンドを調べるのに適していると思います。英文の場合は文字数が少ないと表示される結果が多くなるのではっきりとしたワードで設定する方が使いやすいかもしれません。繰り返しになりますが、日本語マニュアルがインストールされている環境であれば、日本語のキーワードからもコマンド探しができるので便利です。

参考

ちなみに、whatisもaproposもマシンにインストールされているマニュアルを参照しています。

マニュアルを参照するmanコマンドにオプションを組み合わせるとwhatisとaproposとイコールになります。

$ man -f, --whatis whatis と同様
$ man -k, --apropos apropos と同様
$ man -f less
less (1)             - more の反対 (more のようなページャー)
less (3pm)           - perl pragma to request less of something

$ whatis less
less (1)             - more の反対 (more のようなページャー)
less (3pm)           - perl pragma to request less of something

$ man -k less
bzless (1)           - bzip2 圧縮されたテキストを CRT で見るためのファイル閲覧フィルタ
isless (3)           - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
islessequal (3)      - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
islessgreater (3)    - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
less (1)             - more の反対 (more のようなページャー)
lesskey (1)          - less のキー割り当てを指定する
zless (1)            - 圧縮されたテキストを CRT で見るためのファイル閲覧フィルタ
ceil (3)             - ceiling function: smallest integral value not less than argument
ceilf (3)            - ceiling function: smallest integral value not less than argument
ceill (3)            - ceiling function: smallest integral value not less than argument
crda (8)             - send to the kernel a wireless regulatory domain for a given ISO / IEC 3166 alpha2
DBD::Gofer (3pm)     - A stateless-proxy driver for communicating with a remote DBI
grub2-macbless (8)   - Mac-style bless utility for HFS or HFS+
gvfs-less (1)        - Execute less on the output of gvfs-cat
isless (3p)          - test if x is less than y
islessequal (3p)     - test if x is less than or equal to y
islessgreater (3p)   - test if x is less than or greater than y
iw (8)               - show / manipulate wireless devices and their configuration
less (3pm)           - perl pragma to request less of something
lessecho (1)         - expand metacharacters
xzless (1)           - view xz or lzma compressed (text) files
regulatory.bin (5)   - The Linux wireless regulatory database
regulatory.db (5)    - The Linux wireless regulatory database
rfkill (8)           - tool for enabling and disabling wireless devices
tc-nat (8)           - stateless native address translation action
UNIVERSAL (3pm)      - base class for ALL classes (blessed references)

$ apropos less
bzless (1)           - bzip2 圧縮されたテキストを CRT で見るためのファイル閲覧フィルタ
isless (3)           - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
islessequal (3)      - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
islessgreater (3)    - NaN に対して例外を発生せずに、浮動小数点数の大小関係の判定を行う
less (1)             - more の反対 (more のようなページャー)
lesskey (1)          - less のキー割り当てを指定する
zless (1)            - 圧縮されたテキストを CRT で見るためのファイル閲覧フィルタ
ceil (3)             - ceiling function: smallest integral value not less than argument
ceilf (3)            - ceiling function: smallest integral value not less than argument
ceill (3)            - ceiling function: smallest integral value not less than argument
crda (8)             - send to the kernel a wireless regulatory domain for a given ISO / IEC 3166 alpha2
DBD::Gofer (3pm)     - A stateless-proxy driver for communicating with a remote DBI
grub2-macbless (8)   - Mac-style bless utility for HFS or HFS+
gvfs-less (1)        - Execute less on the output of gvfs-cat
isless (3p)          - test if x is less than y
islessequal (3p)     - test if x is less than or equal to y
islessgreater (3p)   - test if x is less than or greater than y
iw (8)               - show / manipulate wireless devices and their configuration
less (3pm)           - perl pragma to request less of something
lessecho (1)         - expand metacharacters
xzless (1)           - view xz or lzma compressed (text) files
regulatory.bin (5)   - The Linux wireless regulatory database
regulatory.db (5)    - The Linux wireless regulatory database
rfkill (8)           - tool for enabling and disabling wireless devices
tc-nat (8)           - stateless native address translation action
UNIVERSAL (3pm)      - base class for ALL classes (blessed references)

公式認定本のwhatisとaproposコマンド解説ページは以下の通りです。