Linux apropos 初学者命令教程(5 个示例)
在此页
- Q1。如何使用 apropos?
- Q2。如何使 apropos 搜索准确的关键字?
- Q3。如何使匹配所有关键字的条目适当显示?
- Q4。如何强制 apropos 不修剪输出?
- Q5。如何解释适当的退出状态?
- 结论
在 Linux 中,如果您需要有关命令的帮助,只需打开它的手册页即可。但是,如果出现需要快速搜索所有可用手册页的名称和描述的情况怎么办?好吧,Linux 已经满足您的要求,因为有一个名为 apropos 的命令可以为您完成这些工作。
在本教程中,我们将使用一些易于理解的示例来讨论 apropos 的基础知识。但在我们这样做之前,值得一提的是,这里的所有示例都已经在 Ubuntu 16.04 LTS 机器上进行了测试。
apropos 命令搜索用户提供的关键字的手册页名称和描述。以下是它的语法:
apropos [OPTIONS] keyword ...
下面是工具手册页关于它的内容:
Each manual page has a short description available within it. apropos
searches the descriptions for instances of keyword.
keyword is usually a regular expression, as if (-r) was used, or may
contain wildcards (-w), or match the exact keyword (-e). Using these
options, it may be necessary to quote the keyword or escape (\) the
special characters to stop the shell from interpreting them.
The standard matching rules allow matches to be made against the page
name and word boundaries in the description.
The database searched by apropos is updated by the mandb program.
Depending on your installation, this may be run by a periodic cron job,
or may need to be run manually after new manual pages have been
installed.
以下是一些 Q&A 风格的示例,应该可以让您对 apropos 命令的工作原理有一个很好的了解。
Q1。如何使用 apropos?
基本用法很简单。只需将要搜索的关键字作为输入传递给 apropos 命令。
例如:
apropos dmesg
产生了以下结果:
dmesg (1) - print or control the kernel ring buffer
当然,您也可以传递多个关键字。
例如:
apropos dmesg whereis
以下是这种情况下的输出:
dmesg (1) - print or control the kernel ring buffer
whereis (1) - locate the binary, source, and manual page files for a...
Q2。如何使 apropos 搜索准确的关键字?
默认情况下,您传递给 apropos 命令的输入不会被精确搜索。例如,如果您将 who 作为输入传递,您还会看到该工具生成的结果包含诸如 whoami 之类的词。

所以这不是一个精确的搜索。但是,您可以使用 -e 或 --exact 命令行选项强制 apropos 搜索精确关键字。

所以现在您看到只有那些与输出中显示的人完全匹配的条目。
Q3.如何使匹配所有关键字的条目适当显示?
如果您将多个关键字作为输入传递给 apropos 命令,该工具将输出匹配/包含至少一个关键字的条目。但是,如果您希望 apropos 仅生成那些匹配/包含所有关键字的条目,请使用 -a 命令行选项。
例如,这是不带 -a 选项的 apropos 命令的输出:

这是启用了 -a 选项的输出:

Q4.如何强制 apropos 不修剪输出?
正如您在之前的问答输出中看到的那样,如果条目太长,该工具会对其进行修剪。例如,请参阅以下输出中突出显示的行:

然而,如果你愿意,你可以强制 apropos 在输出中生成完整的行,你可以使用 -l 命令行选项来做到这一点。

Q5.如何解释适当的退出状态?
apropos 命令产生四种不同的退出状态:0、1、2 和 16。以下是它们各自代表的含义:
0 Successful program execution.
1 Usage, syntax or configuration file error.
2 Operational error.
16 Nothing was found that matched the criteria specified
结论
根据您的工作情况,您可能不需要每天执行 apropos 命令,但正如您现在所了解的那样,在某些情况下它可能是救命稻草。我们在这里讨论了一些使用命令行选项。要了解有关该工具的更多信息,请访问其手册页。