在 Linux 中遨遊手冊頁的海洋

在 Linux 中遨遊手冊頁的海洋 | Linux 中國

Linux 系統上的手冊頁可以做的不僅僅是提供特定命令的信息。它們可以幫助你發現你沒有意識到的命令。-- Sandra Henry-stocker(作者)


手冊頁提供了關於 Linux 命令的基本信息,很多用戶經常參考它,但手冊頁的內容比我們很多人意識到的要多得多。

你總是可以輸入一個像 man who 這樣的命令,然後得到 who 命令的工作原理的漂亮描述,但是探索你可能不知道的命令可能會更有啟發。例如,你可以使用 man 命令來幫助找到一些處理非常具有挑戰性的任務的命令,或者顯示一些選項,這些選項可以幫助你以新的更好的方式使用你已經知道的命令。

讓我們來瀏覽一些選項,看看最終的結果是什麼。

用 man 去識別命令

man 命令可以幫助你按主題查找命令。例如,如果你要找一個計算文件中的行數的命令,你可以提供一個關鍵字。在下面的例子中,我們把關鍵字 “count” 放在了引號中,並添加了空格,這樣我們就不會得到與 “accounts” 或 “accounting” 相關的命令,而是得到那些可以為我們做一些計算的命令。

<code>$ 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/<code>

為了顯示與新用戶賬戶相關的命令,我們可以嘗試使用這樣的命令。

<code>$ 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, …/<code>

需要說明的是,上面的第三項只是提到 “new users” 類似的內容,並不是設置、刪除或配置用戶賬號的命令。man 命令只是在命令描述中匹配了一些詞,作用很像 apropos 命令。注意上面列出的每個命令後面的括號中的數字。這些數字與包含這些命令的手冊頁的分區有關。

確定手冊頁的分區

man 命令的分區將其內容劃分為不同的類別。要列出這些類別,請鍵入 man man,並查看類似下面的描述。你的系統中很可能沒有第 9 分區的命令。

  • 1:可執行程序或 shell 命令
  • 2:系統調用(內核提供的函數)
  • 3:庫調用(程序庫內的函數)
  • 4:特殊文件(通常在可以 /dev 中找到)
  • 5:文件格式和慣例,例如 /etc/passwd
  • 6:遊戲
  • 7:雜項(包括宏包和約定),例如 man(7)、groff(7)
  • 8:系統管理命令(通常只由 root 用戶使用)
  • 9:內核例程(非標準)

手冊頁涵蓋了比我們通常認為的“命令”更多的內容。從上面的描述中可以看到,它們涵蓋了系統調用、庫調用、特殊文件等等。

下面的列表顯示了 Linux 系統中的手冊頁的實際存儲位置。這些目錄上的日期會有所不同,因為隨著更新,其中一些分區會有新的內容,而另一些則不會。

<code>$ 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/<code>

注意,為了節省空間,手冊頁文件一般都是 gzip 壓縮的。每當你使用 man 命令時,man 命令會根據需要解壓。

<code>$ 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/<code>

按分區列出的手冊頁

即使只看第 1 分區的前 10 個手冊頁(如上所示),你也可能會看到一些新的命令 —— 也許是 a2query 或 aaflip(如上所示)。

探索命令的更好策略是按分區列出命令,不查看文件本身,而是使用 man 命令向你顯示命令並提供每個命令的簡要說明。

在下面的命令中,-s 1 指示 man 顯示第 1 分區中的命令信息。-k . 使該命令對所有命令都有效,而不是指定一個特定的關鍵字;如果沒有這個,man 命令就會回過頭來問:“你想要什麼手冊頁?”所以,使用關鍵字來選擇一組相關的命令,或者使用點來顯示一個分區中的所有命令。

<code>$ 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
…/<code>

有多少手冊頁?

如果你對每個分區中有多少手冊頁感到好奇,可以使用以下命令按分區對它們進行計數:

<code>$ for num in {1..8}
> do
>   man -s $num -k . | wc -l
> done
2382
493
2935
53
441
11
245
919/<code>

確切的數量可能有所不同,但是大多數 Linux 系統的命令數量差不多。如果我們使用命令將這些數字加在一起,我們可以看到運行該命令的系統上有將近 7500 個手冊頁。有很多命令,系統調用等。

<code>$ 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/<code>

閱讀手冊頁可以學到很多東西,但是以其他方式瀏覽手冊頁可以幫助你瞭解系統上可能不知道的命令。


via: https://www.networkworld.com/article/3519853/navigating-man-pages-in-linux.html

作者: Sandra Henry-Stocker 選題: lujun9972 譯者: wxy 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: