whichとwhereisの違いって?

6月 21, 2009

Linuxコマンドのwhichとwhereis。その違いがよくわからない。
どちらもコマンドのパスを調べる時に使っているが、実行結果も環境によって変わってくるようだし・・・

$ which grep
/bin/grep

$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz /usr/share/man/man1p/grep.1p.gz


こんなんだったり・・・

$ which grep
/usr/bin/grep

$ whereis grep
/usr/bin/grep


こんなんだったりもする(BSD版)。

ググっていたら、whatisで調べてみなさい、というのがあった。
whatisというコマンドがあるのは、知らなかった。

で、早速聞いてみたら・・・

$ whatis which
which (1) – shows the full path of (shell) commands
which (rpm) – Displays where a particular program in your path is located.

$ whatis whereis
whereis (1) – locate the binary, source, and manual page files for a command
whereis (1) – コマンドのバイナリ・ソース・man ページの場所を示す


と、いうことだそうだ。ふーむ。
ついでにwhatisをwhatisしてみたら・・・

$ whatis whatis
whatis (1) – search the whatis database for complete words
whatis (1) – whatis データベースから完全な単語を検索する


とな。whatis databaseなんてものがあるのか。これも知らなかった。
man whatisしてみたら、

whatis データベースは/usr/sbin/makewhatis コマンドを使用して作成する。


だそうだ。うーむ、makewhatisというコマンドもあるわけか。
ではmakewhatisをwhatisしてみよう。

$ whatis makewhatis
makewhatis (8) – Create the whatis database
makewhatis (8) – whatis データベースを生成する


そのまんまだけど、詳しくはmanに書いてある。
何気ないことから、勉強になりましたw
結論から言うと、コマンドのパスを探すのであればどwhichでもwhereisでもいいワケですね。

追記:「コマンドのパスを探す」という用途に限定するのであれば、whichが本来の
目的に適っているという気がする。

ちなみにどうでもよい補足。BSD版で調べたら以下の結果となった。

$ whatis which
which(1) – locate a program file in the user’s path

$ whatis whereis
whereis(1) – locate programs


うーむ。ちなみにmanだと…

which – locate a command

whereis – locate programs


はっきりいって、これじゃ分かんないヨッw


Categories: Linux