如何使用man查看Linux命令的联机手册

Linux常用的命令有很多的选项,不同的选项组合能达到不同的效果。我们没有必要准确记住这些选项, 只需要对命令的功能有一个大体上的认识,在时隔几周再次使用时,能通过查看联机手册便知道如何使用了。

man 命令行参数

在使用man命令查看帮助时,一般是不需要带什么参数的。不过有些选项对于我们查找文档还是很有帮助的, 下面举例说明几个常用的选项。

关键词查找 -k

在简短描述中查找某关键词。比如我想看一下libcgroup中cgrules的配置相关的帮助页, 但是我不确定名称, 就可以先试着用cgroup作为关键词来查一下:

[wanggan@c4t12190 ~]$ man -k cgroup
cgclassify           (1)  - move running task(s) to given cgroups
cgclear              (1)  - unload the cgroup filesystem
cgconfig.conf [cgconfig] (5)  - libcgroup configuration file
cgcreate             (1)  - create new cgroup(s)
cgred.conf [cgred]   (5)  - libcgroup configuration file
cgrules.conf [cgrules] (5)  - libcgroup configuration file
cgset                (1)  - set the parameters of given cgroup(s)
lscgroup             (1)  - list all cgroups
openshift_cgroup_read_selinux (8)  - Security Enhanced Linux Policy for the openshift_cgroup_read processes

然后再看cgrules.conf:

[wanggan@c4t12190 ~]$ man 5 cgrules.conf

名字匹配 -f

该选项匹配帮助页的名称,比如我们看看帮助文档中以ls开头的:

[wanggan@c4t12190 ~]$ man -f  'ls.*'
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents
lsassd_selinux       (8)  - Security Enhanced Linux Policy for the lsassd processes
lsattr               (1)  - list file attributes on a Linux second extended file system
lsblk                (8)  - list block devices
lscgroup             (1)  - list all cgroups
lscpu                (1)  - CPU architecture information helper
lscpu                (1)  - display information about the CPU architecture
lsdiff               (1)  - show which files are modified by a patch
lsearch              (3)  - linear search of an array
lsearch              (3p)  - linear search and update
lseek                (2)  - reposition read/write file offset
lseek                (3p)  - move the read/write file offset
lseek64              (3)  - reposition 64-bit read/write file offset
lshal                (1)  - list HAL devices
lshw                 (1)  - list hardware
lsinitrd             (1)  - tool to show the contents of an initramfs image
[省略更多输出]

再比如我们想看看对内核模块的操作命令,从帮助文档来讲有哪些:

[wanggan@c4t12190 ~]$ man  -f '.*mod' | grep -i kernel
function::probemod   (3stap)  - Return the probe point's kernel module name
insmod               (8)  - simple program to insert a module into the Linux Kernel
lsmod                (8)  - program to show the status of modules in the Linux Kernel
probe::stap.cache_add_mod [probe::stap] (3stap)  - Adding kernel instrumentation module to cache
rmmod                (8)  - simple program to remove a module from the Linux Kernel

man page页内查找

帮助页面的查找与vim编辑器的查找相似, 比如都支持反斜线(/)查找。拿命令find为例,这个命令是用来查找文件的, 有非常多的选项。假设我知道它支持对查询结果中的文件运行命令,但是不记得具体的语法了,我可以这样查找:

首先,我们通过 --help 看看概要的用法是什么样的:

[wanggan@c4t12190 ~]$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

从以上内容中,我们可以猜测用来执行命令的选项是 -exec, -execdir, -ok等。

我们找找-exec的详细解释:
  1. 输入命令 man find
  2. 输入查询 /-exec. 输入n表示查询下一个匹配项, 输入N表示反向匹配,也就是查询上一个匹配项.

这个例子中,我找到的匹配页如下:

命令find的帮助页中exec选项的说明截图