Linux 哲學:「文檔即程式碼」。善用內建說明系統,比搜尋網路更快、更準、離線可用。本文系統整理 man、info、help、whatis、apropos 五大工具的用法與進階技巧。
man ls # 顯示 ls 手冊
man 3 printf # 顯示第 3 節(函式庫呼叫)的 printf
man -a exit # 依序顯示所有節的 exit
man -k keyword # 關鍵字搜尋(等同 apropos)
man -f ls # 顯示簡短描述(等同 whatis)
| 節 | 內容 | 範例 |
|---|---|---|
| 1 | 使用者指令 | ls, cp, grep |
| 2 | 系統呼叫 | open, read, fork |
| 3 | 函式庫呼叫 | printf, malloc, pthread_create |
| 4 | 特殊檔案/裝置 | /dev/null, /dev/tty |
| 5 | 檔案格式/慣例 | /etc/passwd, crontab |
| 6 | 遊戲 | fortune, snake |
| 7 | 概觀/慣例/協定 | man 7 regex, man 7 ip |
| 8 | 系統管理指令 | systemctl, fdisk, mkfs |
| 9 | 核心常式 | 內核開發參考 |
# 列印為純文字(移除格式控制碼)
man ls | col -b > ls.txt
# 指定語言
man -L zh_TW ls
LC_ALL=zh_TW.UTF-8 man ls
# 搜尋所有手冊頁描述
man -k "file copy"
apropos "file copy"
info # 進入主目錄
info coreutils # 查看 coreutils 完整手冊
info ls # 查看 ls 節點
info -k keyword # 關鍵字搜尋
help # 列出所有內建指令
help cd # 查看 cd 語法
help -m read # 以 man 格式輸出
help -s printf # 簡短用法摘要
常用內建指令:alias, bg, bind, break, builtin, caller, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, false, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, mapfile, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, true, type, ulimit, umask, unalias, unset, wait。
| 指令 | 資料來源 | 搜尋方式 | 輸出 |
|---|---|---|---|
whatis keyword | mandb 資料庫 | 精確匹配指令名 | 單行描述 |
apropos keyword | mandb 資料庫 | 描述文本全文搜尋 | 多行匹配結果 |
whatis ls
# ls (1) - list directory contents
apropos "list directory"
# ls (1) - list directory contents
# dir (1) - list directory contents
# vdir (1) - list directory contents
更新資料庫:mandb(通常由 cron.daily 自動執行)。
khelpcenter / kdehelpgnome-help / yelpxman(X11 版 man 瀏覽器)
# Debian/Ubuntu
apt-get install manpages manpages-zh manpages-posix manpages-dev info-doc
# CentOS/RHEL
yum install man-pages man-pages-zh-CN info
command --help:快速查參數、範例。man command:完整權威參考。info command:GNU 軟體特別詳盡。apropos keyword:關鍵字反查。help builtin:避免混淆外部指令。