情景
互聯網的高速發展給我們的工作和生活都帶來了極大的便利。比如,工作中,當你在linux系統上,遇到一個難題,不知道用什么命令時,借助網絡就可以輕松解決。
這是每個IT人都具備的能力了(搜索也是有方法論的,此處不做討論)。
如果沒有網絡,該怎么辦?或許,你還可以翻閱工具書,請教別人,等等。
但是,如果你只能借助你登錄的系統本身,又該怎么辦?
再或者,即便有網絡,怎樣更快地找到你想要的命令呢?
方案
linux有兩個命令,支持通過關鍵字來搜索命令。
man -k
apropos
$ man man
...(省略)...
-k Equivalent to apropos.
...(省略)...
由此可見,man -k
是完全等價于apropos
的。
$ man apropos
NAME
apropos - search the whatis database for strings
SYNOPSIS
apropos keyword ...
DESCRIPTION
apropos searches a set of database files containing short descriptions of system commands for keywords and displays the result on the standard output.
...(省略)...
由man手冊可知,apropos命令將在whatis database中查詢keyword,并把結果出來。
所以,在不借助網絡的情況下,使用apropos可以縮小你鎖定的范圍,甚至直接幫助你確定命令。‘
實例
以曾經寫過的兩篇文章為例:
既然想要獲取UUID,那我們不妨以UUID來搜索一下:
$ apropos UUID
abrt-action-analyze-oops (1) - Calculate and save UUID and duplicate hash for an oops dump directory DIR
dbus-uuidgen (1) - Utility to generate UUIDs
findfs (8) - Find a filesystem by label or UUID
uuidgen (1) - command-line utility to create a new UUID value
你看,文章中提到的uuidgen
、dbus-uuidgen
命令就在結果中呢。
BTW:一般情況下,apropos的結果中只看第二列為(1)的命令即可。
由文章可知,相關的關鍵字有“進程”、“運行目錄”等,所以不妨使用process、working等關鍵字嘗試。
$ apropos process | fgrep "(1)"
...(省略)...
procmail (1) - autonomous mail processor
ps (1) - report a snapshot of the current processes
pstree (1) - display a tree of processes
pwdx (1) - report current working directory of a process
refer (1) - preprocess bibliographic references for groff
renice (1) - alter priority of running processes
skill (1) - send a signal or report process status
...(省略)...
pwdx命中!根據pwdx的簡介可知,apropos working
也可以定位到它。
總結
其實,apropos的使用包含但不局限于上面的使用場景。
在我看來,apropos好處多多,簡直就是單機版命令搜索引擎:
- 在孤立無援(不能借助網絡、請教別人、翻閱工具書的情況)時,它能給你指明方向,縮小范圍。
- 即使有網絡,當你遇到問題時,順手就可以使用下它,有益無害。
- 給它一個關鍵字,它給你返回包含這個關鍵字的相關命令。比起漫無目的地學習命令,有針對性地學習命令效果會更好。
- 使你更加關注命令的英文描述,不僅可以幫助你精準地知道命令的含義,還無形地幫助你提高英文。
- 忘記了怎么拼寫的命令,不妨用它來幫你鎖定下范圍。
- 以極小的成本讓你增長見識。
實不相瞞,我在知道了這個命令后,經常有意無意地使用它,在有意無意中解決了許多實際問題,也在有意無意中掌握了很多極為實用的命令。
一點經驗:
- 返回結果過多時,可以調整關鍵字,也可以借助grep和正則表達式進一步過濾。
- 鎖定命令的范圍后,結合man命令,效果更佳。