在Linux中遨游手冊(cè)頁(yè)的海洋
Linux 系統(tǒng)上的手冊(cè)頁(yè)可以做的不僅僅是提供特定命令的信息。它們可以幫助你發(fā)現(xiàn)你沒有意識(shí)到的命令。
手冊(cè)頁(yè)提供了關(guān)于 Linux 命令的基本信息,很多用戶經(jīng)常參考它,但手冊(cè)頁(yè)的內(nèi)容比我們很多人意識(shí)到的要多得多。
你總是可以輸入一個(gè)像 man who
這樣的命令,然后得到 who
命令的工作原理的漂亮描述,但是探索你可能不知道的命令可能會(huì)更有啟發(fā)。例如,你可以使用 man
命令來(lái)幫助找到一些處理非常具有挑戰(zhàn)性的任務(wù)的命令,或者顯示一些選項(xiàng),這些選項(xiàng)可以幫助你以新的更好的方式使用你已經(jīng)知道的命令。
讓我們來(lái)瀏覽一些選項(xiàng),看看最終的結(jié)果是什么。
用 man 去識(shí)別命令
man
命令可以幫助你按主題查找命令。例如,如果你要找一個(gè)計(jì)算文件中的行數(shù)的命令,你可以提供一個(gè)關(guān)鍵字。在下面的例子中,我們把關(guān)鍵字 “count” 放在了引號(hào)中,并添加了空格,這樣我們就不會(huì)得到與 “accounts” 或 “accounting” 相關(guān)的命令,而是得到那些可以為我們做一些計(jì)算的命令。
$ man -k ' count '
anvil (8postfix) - Postfix session count and request rate control
cksum (1) - checksum and count the bytes in a file
sum (1) - checksum and count the blocks in a file
timer_getoverrun (2) - get overrun count for a POSIX per-process timer
為了顯示與新用戶賬戶相關(guān)的命令,我們可以嘗試使用這樣的命令。
$ man -k "new user"
newusers (8) - update and create new users in batch
useradd (8) - create a new user or update default new user information
zshroadmap (1) - informal introduction to the zsh manual The Zsh Manual, …
需要說(shuō)明的是,上面的第三項(xiàng)只是提到 “new users” 類似的內(nèi)容,并不是設(shè)置、刪除或配置用戶賬號(hào)的命令。man
命令只是在命令描述中匹配了一些詞,作用很像 apropos
命令。注意上面列出的每個(gè)命令后面的括號(hào)中的數(shù)字。這些數(shù)字與包含這些命令的手冊(cè)頁(yè)的分區(qū)有關(guān)。
確定手冊(cè)頁(yè)的分區(qū)
man
命令的分區(qū)將其內(nèi)容劃分為不同的類別。要列出這些類別,請(qǐng)鍵入 man man
,并查看類似下面的描述。你的系統(tǒng)中很可能沒有第 9 分區(qū)的命令。
1
:可執(zhí)行程序或 shell 命令2
:系統(tǒng)調(diào)用(內(nèi)核提供的函數(shù))3
:庫(kù)調(diào)用(程序庫(kù)內(nèi)的函數(shù))4
:特殊文件(通常在可以/dev
中找到)5
:文件格式和慣例,例如/etc/passwd
6
:游戲7
:雜項(xiàng)(包括宏包和約定),例如man
(7)、groff
(7)8
:系統(tǒng)管理命令(通常只由 root 用戶使用)9
:內(nèi)核例程(非標(biāo)準(zhǔn))
手冊(cè)頁(yè)涵蓋了比我們通常認(rèn)為的“命令”更多的內(nèi)容。從上面的描述中可以看到,它們涵蓋了系統(tǒng)調(diào)用、庫(kù)調(diào)用、特殊文件等等。
下面的列表顯示了 Linux 系統(tǒng)中的手冊(cè)頁(yè)的實(shí)際存儲(chǔ)位置。這些目錄上的日期會(huì)有所不同,因?yàn)殡S著更新,其中一些分區(qū)會(huì)有新的內(nèi)容,而另一些則不會(huì)。
$ ls -ld /usr/share/man/man?
drwxr-xr-x 2 root root 98304 Feb 5 16:27 /usr/share/man/man1
drwxr-xr-x 2 root root 65536 Oct 23 17:39 /usr/share/man/man2
drwxr-xr-x 2 root root 270336 Nov 15 06:28 /usr/share/man/man3
drwxr-xr-x 2 root root 4096 Feb 4 10:16 /usr/share/man/man4
drwxr-xr-x 2 root root 28672 Feb 5 16:25 /usr/share/man/man5
drwxr-xr-x 2 root root 4096 Oct 23 17:40 /usr/share/man/man6
drwxr-xr-x 2 root root 20480 Feb 5 16:25 /usr/share/man/man7
drwxr-xr-x 2 root root 57344 Feb 5 16:25 /usr/share/man/man8
注意,為了節(jié)省空間,手冊(cè)頁(yè)文件一般都是 gzip 壓縮的。每當(dāng)你使用 man
命令時(shí),man
命令會(huì)根據(jù)需要解壓。
$ ls -l /usr/share/man/man1 | head -10
total 12632
lrwxrwxrwx 1 root root 9 Sep 5 06:38 [.1.gz -> test.1.gz
-rw-r--r-- 1 root root 563 Nov 7 05:07 2to3-2.7.1.gz
-rw-r--r-- 1 root root 592 Apr 23 2016 411toppm.1.gz
-rw-r--r-- 1 root root 2866 Aug 14 10:36 a2query.1.gz
-rw-r--r-- 1 root root 2361 Sep 9 15:13 aa-enabled.1.gz
-rw-r--r-- 1 root root 2675 Sep 9 15:13 aa-exec.1.gz
-rw-r--r-- 1 root root 1142 Apr 3 2018 aaflip.1.gz
-rw-r--r-- 1 root root 3847 Aug 14 10:36 ab.1.gz
-rw-r--r-- 1 root root 2378 Aug 23 2018 ac.1.gz
按分區(qū)列出的手冊(cè)頁(yè)
即使只看第 1 分區(qū)的前 10 個(gè)手冊(cè)頁(yè)(如上所示),你也可能會(huì)看到一些新的命令 —— 也許是 a2query
或 aaflip
(如上所示)。
探索命令的更好策略是按分區(qū)列出命令,不查看文件本身,而是使用 man
命令向你顯示命令并提供每個(gè)命令的簡(jiǎn)要說(shuō)明。
在下面的命令中,-s 1
指示 man
顯示第 1 分區(qū)中的命令信息。-k .
使該命令對(duì)所有命令都有效,而不是指定一個(gè)特定的關(guān)鍵字;如果沒有這個(gè),man
命令就會(huì)回過(guò)頭來(lái)問(wèn):“你想要什么手冊(cè)頁(yè)?”所以,使用關(guān)鍵字來(lái)選擇一組相關(guān)的命令,或者使用點(diǎn)來(lái)顯示一個(gè)分區(qū)中的所有命令。
$ man -s 1 -k .
2to3-2.7 (1) - Python2 to Python3 converter
411toppm (1) - convert Sony Mavica .411 image to ppm
as (1) - the portable GNU assembler.
baobab (1) - A graphical tool to analyze disk usage
busybox (1) - The Swiss Army Knife of Embedded Linux
cmatrix (1) - simulates the display from "The Matrix"
expect_dislocate (1) - disconnect and reconnect processes
red (1) - line-oriented text editor
enchant (1) - a spellchecker
…
有多少手冊(cè)頁(yè)?
如果你對(duì)每個(gè)分區(qū)中有多少手冊(cè)頁(yè)感到好奇,可以使用以下命令按分區(qū)對(duì)它們進(jìn)行計(jì)數(shù):
$ for num in {1..8}
> do
> man -s $num -k . | wc -l
> done
2382
493
2935
53
441
11
245
919
確切的數(shù)量可能有所不同,但是大多數(shù) Linux 系統(tǒng)的命令數(shù)量差不多。如果我們使用命令將這些數(shù)字加在一起,我們可以看到運(yùn)行該命令的系統(tǒng)上有將近 7500 個(gè)手冊(cè)頁(yè)。有很多命令,系統(tǒng)調(diào)用等。
$ for num in {1..8}
> do
> num=`man -s $num -k . | wc -l`
> tot=`expr $num + $tot`
> echo $tot
> done
2382
2875
5810
5863
6304
6315
6560
7479 <=== total
閱讀手冊(cè)頁(yè)可以學(xué)到很多東西,但是以其他方式瀏覽手冊(cè)頁(yè)可以幫助你了解系統(tǒng)上可能不知道的命令。