Linux中Man命令简介
1. 概述
有时,在命令行界面中,我们希望查看特定命令的文档。
在这个快速教程中,我们将讨论如何使用man 命令获取 Linux 中命令的信息。
2. man 页组织结构
man命令用于显示命令的用户手册。
通常,给man的参数是程序、实用程序或函数的名称。因此,然后找到并显示与这些参数中的每一个相关的手册页。
2.1. 编号部分
通常,系统手册页的集合被划分为编号的部分。这些部分编号和相应的页面类型是:
- 可执行程序或命令
- 系统调用(内核提供的函数)
- 库调用(库提供的函数)
- 特殊文件(通常在*/dev*中找到)
- 文件格式和约定(配置文件,例如etc/passwd)
- 游戏(例如基于角色的应用程序)
- 杂项(例如人(7))
- 系统管理命令(通常仅适用于 root)
- 内核例程(非标准)
2.2. 每个man页中的命名部分
在每个手册页中,有许多命名的部分。默认情况下,这些包括NAME、SYNOPSIS、CONFIGURATION、DESCRIPTION、OPTIONS、EXIT STATUS、RETURN VALUE和ERRORS。
此外,手册页可以包括名为ENVIRONMENT、FILES、VERSIONS、CONFORMING TO、NOTES、BUGS、EXAMPLE、AUTHORS和SEE ALSO的部分。
3. 语法
现在,让我们看一下man命令的基本语法:
$ man [option].. [command name]..
例如,要获取有关passwd命令的信息,我们可以使用:
$ man passwd
PASSWD(1) User utilities PASSWD(1)
NAME
passwd - update user's authentication tokens
SYNOPSIS
passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]
DESCRIPTION
The passwd utility is used to update user's authentication token(s).
This task is achieved through calls to the Linux-PAM and Libuser API. Essentially, it initializes itself as a "passwd"
service with Linux-PAM and utilizes configured password modules to authenticate and then update a user's password.
3.1. 在手册页中按关键字搜索
我们可以使用 -k 选项获得与所有手册页中的关键字或正则表达式匹配的结果。
现在,让我们看看使用 -k 选项可以找到什么:
$ man -k printf
asprintf (3) - print to allocated string
dprintf (3) - formatted output conversion
dprintf (3p) - print formatted output
fprintf (3) - formatted output conversion
在这里,我们看到所有返回匹配printf作为正则表达式的手册页的列表。
3.2. 按顺序显示命令的所有部分
在默认模式下,man命令只显示最相关的手册页。我们可以**使用*-a选项让man*按顺序显示所有匹配的手册页。**让我们看看如何使用此选项列出所有匹配的手册页:
$ man -a intro
--Man-- next: intro(2) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(3) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(4) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(6) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(7) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: intro(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
因此,intro命令的所有相关手册页都会依次显示。
3.3. 显示特定编号的部分
众所周知,手册分为多个部分。我们可以将章节号作为参数传递给给定命令的手册的特定章节。让我们看一个例子——我们将查找intro命令的第 2 部分:
$ man 2 intro
INTRO(2) Linux Programmer's Manual
INTRO(2)
NAME
intro - introduction to system calls
DESCRIPTION
Section 2 of the manual describes the Linux system calls. A system call is an entry point into the Linux kernel. Usually,
system calls are not invoked directly:instead, most system calls have corresponding C library wrapper functions which
perform the steps required (e.g., trapping to kernel mode) in order to invoke the system call.
Thus, making a system call looks the same as invoking a normal library function.
3.4. 查找命令手册页的文件系统位置
此外,我们可以使用*-w*选项在文件系统中找到手册页:
$ man -w ping
/usr/share/man/man8/ping.8.gz
如上所示,我们可以看到ping命令手册页的文件系统位置。
最后,我们可以阅读使用man本身的man命令:
$ man man