はじめに

こんにちは!今回はwhatisコマンドについて解説します。

whatisは、コマンドやファイルの簡単な説明を表示するコマンドです。「このコマンド、何をするの?」と思ったときに即座に答えてくれる超便利なやつ。manコマンドで長〜いドキュメントを読むより、さっと概要を知りたい時に活躍します。

調べたいコマンドが何をするのか、ざっと知りたい時に重宝しますよ。

whatisコマンドとは

whatisは、コマンドやプログラムの簡潔な説明を表示する外部コマンドです。manデータベースから情報を抽出して表示します。

manコマンドは詳細なドキュメント全体を表示しますが、whatisはその冒頭の一行説明だけを抽出します。つまり「このコマンドが何をするのか」を素早く知ることができるんですね。

基本構文

1
whatis [オプション] [コマンド名 ...]

複数のコマンドを一度に調べることもできます。

主なオプション

オプション 説明
-d デバッグ情報を表示
-l 長い説明を表示(manコマンドのように)
-r 正規表現で検索
-w ワイルドカード検索
-k キーワード検索(aproposと同じ)

使用例

例1: 基本的な使い方

1
whatis ls

実行結果:

1
ls (1)               - list directory contents

lsコマンドの説明が一行で表示されます。

例2: 複数のコマンドを調べる

1
whatis grep sed awk

実行結果:

1
2
3
grep (1)             - print lines matching a pattern
sed (1)              - stream editor for filtering and transforming text
awk (1)              - pattern scanning and processing language

3つのコマンドの説明が表示されます。

例3: 組み込みコマンドを調べる

1
whatis cd

実行結果:

1
cd (1)               - change the working directory

シェル組み込みコマンドの説明も表示可能です。

例4: ファイルコマンドを調べる

1
whatis file

実行結果:

1
file (1)             - determine file type

fileコマンドについて。

例5: 存在しないコマンドを調べる

1
whatis invalid_command

実行結果:

1
invalid_command: nothing appropriate

存在しないコマンドを調べるとエラーメッセージが出ます。

例6: 正規表現で検索

1
whatis -r "^zip"

実行結果:

1
2
3
4
zip (1)              - package and compress (archive) files
zipcmp (1)           - compare files in zip archives
zipgrep (1)          - search files in a zip archive for lines matching a pattern
zipinfo (1)          - list detailed information about a ZIP archive

“zip"で始まるすべてのコマンドが表示されます。

例7: ワイルドカードで検索

1
whatis -w "*zip*"

実行結果:

1
2
3
4
5
bzip2 (1)            - a block-sorting file compressor
funzip (1)           - filter for extracting from a zip archive
gunzip (1)           - compress or expand files
unzip (1)            - list, test and extract compressed files
zip (1)              - package and compress (archive) files

名前に"zip"を含むすべてのコマンド。

例8: キーワード検索

1
whatis -k compress

実行結果:

1
2
3
4
5
6
7
bzip2 (1)            - a block-sorting file compressor
compress (1)         - compress files
gzip (1)             - compress or expand files
tar (1)              - manipulate archives
uncompress (1)       - compress files
xz (1)               - Compress or decompress .xz and .lzma files
zip (1)              - package and compress (archive) files

“compress"というキーワードを含むすべてのコマンドが表示されます。

例9: manセクション指定で検索

1
whatis printf

実行結果:

1
2
printf (1)           - format and print data
printf (3)           - formatted output conversion

異なるセクションの同名コマンドが表示されます。

例10: 長い説明を表示

1
whatis -l ls

実行結果:

1
2
3
4
ls (1)               - list directory contents
        List information about the FILEs (the current directory by
        default).  Sort entries alphabetically if none of the -cftuvSUX
        nor --sort options are specified.

マニュアルからより詳しい説明が取得されます。

Tips・注意点

manデータベースの更新

whatis は manページのデータベースから情報を取得します。古い環境では手動更新が必要です。

1
sudo mandb  # manデータベースを更新

aproposとの違い

1
2
3
4
5
# whatis: コマンド名で直接検索(正確)
whatis ls

# apropos: キーワード含有を検索(広い)
apropos list

whatisは厳密な検索、aproposは曖昧検索。

正規表現オプション

1
2
3
4
5
# ドット(.)が任意の文字にマッチ
whatis -r "...sh"  # bash, dash, ksh など

# キャレットで開始位置指定
whatis -r "^grep"  # grep で始まるコマンド

正規表現は強力です。

セクション番号の意味

1
2
3
4
# セクション 1: ユーザーコマンド
# セクション 3: C言語ライブラリ関数
# セクション 5: ファイルフォーマット
# セクション 8: システム管理コマンド

括弧内の数字がセクション番号です。

実践的な使い方

不慣れなコマンドをさっと確認

1
2
3
4
5
whatis stat
# stat (1)             - display file status

whatis uniq
# uniq (1)             - report or omit repeated lines

未知のコマンドに出会ったら、まずwhatis。

スクリプト内で使い方を参考にしたい時

1
2
3
whatis -r "^chmod"
whatis -r "^chown"
whatis -r "^chgrp"

権限関連のコマンド一覧を素早く取得。

ショートカットとしてaliasを設定

1
2
alias wh='whatis'
wh ls   # コマンド短縮

频繁に使う場合はエイリアスが便利。

ログを見ながら不明なコマンドを調査

1
2
3
# ログに出てきた "pgrep" が何かわからない時
whatis pgrep
# pgrep (1)           - look up processes based on name and other attributes

ログやエラーメッセージからわからないコマンドをすぐに調べられます。

まとめ

whatisコマンドのポイント:

  • コマンドの簡潔な説明を一行で表示
  • manデータベースから情報を抽出
  • manより高速で軽量
  • -r: 正規表現検索
  • -w: ワイルドカード検索
  • -k: キーワード検索(apropos同等)
  • -l: 長い説明を表示
  • よく使う組み合わせ: whatis [コマンド], whatis -r "^pattern"

わからないコマンドにぶつかった時は、manで長〜いドキュメントを読む前に、まずwhatisで「何をするコマンド?」を確認する癖をつけると、効率よく学習できますよ!