linux man命令_如何使用Linux的man命令:隐藏的秘密和基础

linux man命令

linux man命令

A terminal prompt on a Linux laptop computer.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

Everything you need to know about Linux is in its man pages. For example, you can search for a command to accomplish a task, even if you don’t know what it’s called. But how can you find the man pages? Here are some tricks.

您需要了解的有关Linux的所有信息都在其手册页中。 例如,即使您不知道该命令的名称,也可以搜索该命令来完成任务。 但是如何找到手册页? 这里有一些技巧。

Linux的内置手册 (Linux’s Built-in Manual)

There’s an old (from the Unix golden era) Linux joke that the only command you need to know is man, the system entry point to the user manual. There’s a smidgen of truth in this, but even man can be confusing at first. Or, more accurately, finding the information you need can be confusing.

有一个古老的(来自Unix黄金时代)Linux笑话, 唯一需要知道的命令man即用户手册的系统入口点 。 这有一个真理的诡计,但即使是man也会令人困惑。 或者,更准确地说,找到所需的信息可能会造成混淆。

Have you ever known what you wanted to do, but didn’t know the name of the command that would get the task done? We’ve probably all been there. It’s like trying to look up a word in the dictionary when you don’t know the word.

您是否知道您想做什么,但不知道可以完成任务的命令名称? 我们可能都去过那里。 这就像在您不知道单词时尝试在字典中查找单词。

So, how can you find what you’re looking for? Well, there are ways around this quandary with man.

那么,如何找到所需的内容? 好吧,有很多办法可以解决这个与man难题。

The numbers are another freshman issue. What are they, and what do they mean? You’ll see things like man(2) or man(5) quoted in the documentation and on the internet. You’ll see references to commands followed by numbers, too, like mount(2) and mount(8). Surely there can’t be more than one mount command, right? As we’ll see, the numbers are important and relatively simple.

这些数字是新生的另一个问题。 它们是什么,它们是什么意思? 您会在文档和互联网上看到诸如man(2)man(5)之类的内容。 您还将看到对命令的引用,后跟数字,例如mount(2)mount(8) 。 当然不能有多个mount命令,对吗? 正如我们将看到的,这些数字很重要,而且相对简单。

Speaking of simple, searching within man is pretty easy once you know how to do it. In fact, there are some neat ways you can search and navigate within man. Let’s fire it up and take a look!

说到简单,在man内进行搜索就很容易了。 实际上,您可以在man内搜索和导航一些巧妙的方法。 让我们来看看吧!

如何打开手册 (How to Open the Manual)

To use man, you type man on the command line, followed by a space and a Linux command. man opens the Linux manual to the “man page” that describes that command—if it can find it, of course.

要使用man ,请在命令行上输入man ,后跟一个空格和一个Linux命令。 man将Linux手册打开到描述该命令的“手册页” —当然可以找到它。

Let’s type the following and see what man says about man:

让我们输入以下内容,看看manman

man man
The "man man" command in a terminal window.

The man page for man opens.

对于该名男子页man打开。

The man page for "man 1" open in a terminal window.

As you can see, this is the man(1) page.

如您所见,这是man(1)页面。

Follow these tips to navigate the page:

请按照以下提示浏览页面:

  • To move through the man page one line at a time: Use the scroll wheel on your mouse, or the Up and Down arrow and Enter keys.

    要一次在手册页中移动:使用鼠标上的滚轮,或使用上下箭头和Enter键。

  • To move through the man page one screen at a time: Press the Space bar, and the PgDn and PgUp keys.

    要一次在一个页面上浏览手册页,请执行以下操作:按空格键以及PgDn和PgUp键。

  • To move directly to the top or bottom of the man page: Press the Home and End keys.

    要直接移至手册页的顶部或底部:按Home和End键。

If you press H, you enter the help section and see a table of alternate keystrokes you can use. Those listed above will probably feel more natural to most people.

如果按H键,则进入帮助部分,并查看可使用的其他击键表。 上面列出的那些对于大多数人而言可能会更自然。

To exit man, just press Q.

要退出man ,只需按Q。

男人的解剖 (The Anatomy of a man Page)

At the top of the page, you see the headings “Name” and “Synopsis.” There’s a convention to the layout of man pages. There are man pages for commands, programs, and programming functions (library routines). You won’t see all of these headings on every man page, because some of them only apply to certain types of commands.

在页面顶部,您会看到标题“名称”和“提要”。 手册页的布局有一个约定。 有用于命令,程序和编程功能(库例程)的手册页。 您不会在每个手册页上看到所有这些标题,因为其中一些仅适用于某些类型的命令。

The following are some of the headings you might see:

以下是您可能会看到的一些标题:

  • Name: The name of the command the man page is describing.

    名称:手册页正在描述的命令的名称。

  • Synopsis: A summary of the command and its syntax.

    概要:命令及其语法的摘要。

  • Configuration: Configuration details for a device.

    配置:设备的配置详细信息。

  • Description: An explanation of what the program does.

    说明:程序功能的说明。

  • Options: A description of the command-line options the command accepts.

    选项:命令接受的命令行选项的描述。

  • Exit Status: Possible exit status values for the command, and what might cause them to be used.

    退出状态:命令的可能退出状态值,以及可能导致它们使用的原因。

  • Return Value: If the man page is for a library routine, this describes the value the library routine can send back to the function that called that routine.

    返回值:如果手册页用于库例程,则它描述库例程可以发送回调用该例程的函数的值。

  • Errors: A list of the values that might be placed in errno in the event of an error.

    错误:发生错误时可能放置在errno 中的值的列表。

  • Environment: A list of the environment variables that affect the command or program, and in what way.

    环境:影响命令或程序以及以何种方式影响环境变量的列表。

  • Files: A list of the files the command or program uses, such as configuration files.

    文件:命令或程序使用的文件列表,例如配置文件。

  • Attributes: A summary of various attributes of the command.

    属性:命令各种属性的摘要。

  • Versions: Details of the Linux kernel or library versions where a system call or library function first appeared or changed significantly from previous versions.

    版本:系统内核或库函数首次出现或与先前版本相比有显着变化Linux内核或库版本的详细信息。

  • Conforming to: A description of any standards with which the command might comply, such as POSIX.

    符合:命令可能遵循的任何标准的描述,例如POSIX

  • Notes: Miscellaneous notes.

    注意事项:杂项注意事项。

  • Bugs: Known issues.

    错误:已知问题。

  • Examples: One or more examples demonstrating the use of the command.

    示例:一个或多个演示该命令用法的示例。

  • Authors: The people who wrote or maintain the command.

    作者:编写或维护命令的人员。

  • See also: Recommended reading related to the command or topic.

    另请参阅:与命令或主题相关的推荐读物。

男人节 (The man Sections)

If you scroll down a couple of pages, you see a list of the sections in the manual.

如果向下滚动几页,则会看到手册中各节的列表。

Manual sections listed in "man" in a terminal window.

The sections are:

这些部分是:

  1. General commands: Commands you use on the command line.

    常规命令:在命令行上使用的命令。

  2. System calls: Functions the kernel provides that a program can call.

    系统调用:内核提供的程序可以调用的功能。

  3. Library functions: Functions programs can call in code libraries (mainly the C standard).

    库函数:函数程序可以调用代码库(主要是C标准)。

  4. Special files: Usually devices, such as those found in /dev, and their drivers.

    特殊文件:通常是设备,例如在/ dev中找到的设备及其驱动程序。

  5. File formats and conventions: Formats for files, such as the passwdcron table, and tar archive files.

    文件格式和约定:文件格式 ,例如passwdcron表和tar存档文件。

  6. Games: Descriptions of commands, like fortune, that display quotes from a database when you run them.

    游戏:命令的描述,例如fortune ,在运行命令时显示数据库中的引号。

  7. Miscellaneous: Descriptions of things like inodes, boot parameters, and man itself.

    其他:诸如inode,引导参数和man本身的描述。

  8. System administration: Commands and daemons usually reserved for root to work with.

    系统管理:命令和守护程序通常保留给root使用。

  9. Kernel Routines: Information related to the internal operation of the kernel. This includes function interfaces and variables useful to programmers who are writing device drivers, for example. On most systems, this section isn’t installed.

    内核例程:内核的内部操作有关的信息。 例如,这包括对编写设备驱动程序的程序员有用的函数接口和变量。 在大多数系统上,未安装此部分。

When you see a command followed by a number, it’s referring to the description of that command in that section of the manual. For example, man(1) refers to the entry in section one of the manual describing the man command.

当您看到一个命令后跟一个数字时,它指的是手册那部分中对该命令的描述。 例如, man(1)是指手册第一节中描述man命令的条目。

In the image above, you see a reference to man(7). This means there’s more information about man in another section. When we first opened the man page, it displayed man(1). If you type only man with no section number, man searches all the sections in order, looking for an entry for the command you typed. Of course, it found man(1) before man(7).

在上图中,您看到了对man(7)的引用。 这意味着在另一部分中将有更多关于man的信息。 当我们第一次打开手册页时,它显示man(1) 。 如果只键入man没有节号, man为了搜索所有章节,找你键入的命令的条目。 当然,它在man(7)之前找到了man(1) man(7)

If you want to force man to find an entry from a specific section, you have to include the section number on the command line.

如果要强制man从特定部分中查找条目,则必须在命令行中包括该部分号。

For example, we type the following to open the entry for man in section seven:

例如,我们键入以下内容以打开第七部分中的man条目:

man 7 man
The "man 7 man" command in a terminal window.

The manual opens to the man entry in section seven.

手册将在第七部分打开该手册条目。

A "man" page open to section seven of the manual in a terminal window.

This man page provides instructions for writing man pages. It describes the file format and macros you can use to do some of the work for you. The man(1) page in section one we looked at earlier described how to use man itself.

该手册页提供了有关编写手册页的说明。 它描述了可以用来为您做一些工作的文件格式和宏。 我们前面看过的第一部分的man(1)页面描述了如何使用man本身。

如何在节中查找条目 (How to Find Entries in Sections)

Normally, if you simply want to know how to use a command, you don’t have to give a section number. man will find the standard entry that describes how to use that command in section one of the manual. Sometimes, though, you do need to open a command entry in a particular section because you want different information.

通常,如果您只是想知道如何使用命令,则不必提供段号。 man将找到描述如何使用该命令的标准条目。 但是,有时您确实需要在特定部分中打开命令条目,因为您需要其他信息。

You can easily find out which sections of the manual contain entries for a command. Each man page has a title and a short description. The -f (whatis) option searches the page titles and returns a list of matches.

您可以轻松地找到手册的哪些部分包含命令条目。 每个手册页都有标题和简短描述。 -f (whatis)选项搜索页面标题并返回匹配项列表。

For this example, we type the following:

对于此示例,我们键入以下内容:

man -f man
The "man -f man" command in a terminal window.

The two man pages for man are listed together with their section numbers and short descriptions. Be careful, though—some entries have the same name, but describe different commands and functions.

列出了man的两个手册页以及它们的节号和简短描述。 但是要小心-有些条目具有相同的名称,但是描述的命令和功能却不同。

For example, we type the following:

例如,我们键入以下内容:

man -f printf
The "man -f printf" command in a terminal window.

It appears that two entries were found for printf: the first in section one, and another in section three. However, these are different commands. The man page in section one describes the command line printf command, which formats output in the terminal window. The man page in section three describes the printf  family of library functions in the C programming language.

似乎在printf中找到了两个条目:第一个部分中的第一个,第三部分中的另一个。 但是,这些是不同的命令。 第一部分的手册页描述了命令行printf命令,该命令格式化了在终端窗口中输出的格式 。 第三节的手册页介绍了使用C编程语言编写 的库函数printf 系列

It’s also possible to search through the short descriptions, as well as the page titles. To do so, you use the -k (apropos) option. This will also match occurrences of the search term inside other, longer, words.

还可以搜索简短描述以及页面标题。 为此,请使用-k (apropos)选项。 这也将匹配其他较长单词内搜索词的出现。

We type the following:

我们输入以下内容:

man -k printf
The "man -k printf" command in a terminal window.

Many of these commands are described in the same few man pages because their core functionality is mainly the same. The man page for vprintf describes the functionality of 10 of the commands listed in the image above.

由于这些命令的核心功能基本相同,因此在少数几个手册页中对其进行了介绍。 vprintf的手册页描述了上图中列出的10个命令的功能。

You can leverage this functionality to search for information related to what you’re trying to achieve, even if you don’t know the name of the command you want to use.

即使您不知道要使用的命令的名称,也可以利用此功能来搜索与您要实现的目标有关的信息。

Let’s say you want to change the password of a user account. We can search for any commands that mention “user” in the man page titles or descriptions. We can then pipe it through grep to look for entries that contain “password.”

假设您要更改用户帐户的密码。 我们可以搜索手册页标题或描述中提及“ user”的任何命令。 然后,我们可以通过grep将其通过管道查找包含“密码”的条目。

To do this, we type the following:

为此,我们键入以下内容:

man -k 'user ' | grep password
The "man -k 'user ' | grep password" command in a terminal window.

Because we enclosed the word “user” in single quotes and included a space at the end, it will only find matches for “user,” not “users.” A quick glance through the search results shows us the likely candidate is passwd.

因为我们将“用户”一词用单引号引起来,并且在末尾加了一个空格,所以它只会找到“用户”而不是“用户”的匹配项。 快速浏览一下搜索结果,就可以发现可能的候选人是passwd

Because it’s a section one man page and we don’t need to include the section number in the command, we type the following:

因为这是一节的手册页,并且我们不需要在命令中包括节号,所以我们键入以下内容:

man passwd

Say we need a command that counts the number of words in a text file. We type the following to see whether something like that exists:

假设我们需要一个命令来计算文本文件中单词的数量。 我们键入以下内容以查看是否存在类似的内容:

man -k word | grep count
The "man -k word | grep count" command in a terminal window.

To find out everything there is to know about counting words, we type this command:

为了找出关于单词计数的所有知识,我们键入以下命令:

man wc

Speaking of wc, we can also use the -k (apropos) option with a single period (.) as the search target, which will match everything. If we pipe that through wc and use the -l (lines) option, it’ll tell us how many man pages there are on the computer.

说到wc ,我们还可以使用带有单个句点( . )的-k (apropos)选项作为搜索目标,它将匹配所有内容。 如果我们通过wc传递该消息并使用-l (行)选项,它将告诉我们计算机上有多少手册页。

To do all of this, we type the following command:

为此,我们键入以下命令:

man -k . | wc -l
The "man -k . | wc -l" command in a terminal window.

There are 6,706 man pages on this Ubuntu computer, but don’t be surprised if the number is different on yours. It can vary depending on which software packages and utilities you have on your machine, and which man pages were pre-installed.

这台Ubuntu计算机上有6,706个手册页,但是如果您的页数不相同,请不要感到惊讶。 它可能会有所不同,具体取决于您计算机上安装的软件包和实用程序以及预安装的手册页。

在手册页中搜索 (Searching In a man Page)

You can also search forward or backward from your current position inside a man page.

您还可以在手册页中从当前位置向前或向后搜索。

For this example, we type the following to open the man page for the history command:

对于此示例,我们键入以下内容以打开history命令的手册页:

man history
The "man history" command in a terminal window.

To search forward, we press the forward slash (/), and then type the word “event.” The search target appears at the bottom of the terminal window, and we press Enter to start the search.

若要向前搜索,请按正斜杠( / ),然后键入单词“ event”。 搜索目标出现在终端窗口的底部,然后按Enter键开始搜索。

The "/event" command on the "history man" page in a terminal window.

The window displays the first result of any matches found, and they’re highlighted.

窗口显示找到的所有匹配项的第一个结果,并突出显示它们。

A "history man" page with search results for "event" highlighted in a terminal window.

Press “n” to move from result to result toward the bottom of the page. To search backward through the man page, press “N”; this will move you back to the top of the page.

按“ n”将结果从结果移到页面底部。 要在手册页中向后搜索,请按N。 这会将您移回页面顶部。

To toggle the highlighting on and off, press Esc+U.

要打开和关闭突出显示,请按Esc + U。

If you’re near the bottom of the man page and want to search upward, press the question mark (?), and then type the search term. We searched for “entry.”

如果您在手册页底部附近并且想要向上搜索,请按问号(?),然后键入搜索词。 我们搜索了“ entry”。

A "?entry" search term to search upward in a man page in a terminal window.

Again, any matching results are highlighted.

同样,任何匹配的结果都将突出显示。

Results for the search term "entry" highlighted in a man page in a terminal window.

To search for the next matching result, press “n” to move toward the beginning of the man page. Press “N” to go to the previous matching result and move toward the end of the man page.

要搜索下一个匹配结果,请按“ n”移至手册页的开头。 按“ N”转到上一个匹配结果,然后移至手册页的末尾。

There’s another way you can search a man page. It hides all lines that don’t contain a match to your search term, so it’s best to use line numbers with this technique.

还有另一种搜索手册页的方法。 它会隐藏所有不包含与您的搜索字词匹配的行,因此最好将行号与该技术结合使用。

If we type “-N” and hit Enter, we can see the line numbers in the man page.

如果键入“ -N”并按Enter,则可以在手册页中看到行号。

A man page with line numbers in a terminal window.

We press the ampersand (&), type our search term (number), and then hit Enter.

我们按与号(&),键入搜索词(数字),然后按Enter。

A "&/number" matching line search in a man page in a terminal window.

Only the lines that contain our search term are displayed.

仅显示包含我们搜索词的行。

Lines that contain the word "number" in a man page in a terminal window.

It’s easy to skim these and spot any that look interesting. We think line 292 looks promising, so we want to go to that section of the man page and check it out.

可以轻松浏览这些内容,并发现任何有趣的内容。 我们认为第292行看起来很有希望,因此我们想转到手册页的这一部分并进行检查。

To see all the lines again, we hit the ampersand (&), and then press Enter.

要再次查看所有行,我们点击与号(&),然后按Enter。

The "&/" command to exit from the matching lines display in a man page in a terminal window.

We type “292,” and then “g” to go to that line.

我们键入“ 292”,然后键入“ g”以转到该行。

A command to move to line "292" in a man page in a terminal window.

As soon as we type “g,” we’re taken to line 292 (which is why the “g” doesn’t appear in the image above). Line 292 is then displayed at the top of the terminal window.

键入“ g”后,我们将转到第292行(这就是为什么“ g”没有出现在上图中的原因)。 然后,第292行显示在终端窗口的顶部。

Line 292 at the top of the screen in a man page in a terminal window.

You can press “-n” and hit Enter to remove the line numbers.

您可以按“ -n”并按Enter删除行号。

阅读神话般的手册 (Read the Fabulous Manual)

There’s a wealth of information in the man pages. Even with commands you think you know well, it’s a safe bet there are other options you’ve never heard of.

手册页中有很多信息。 即使使用您认为自己很熟悉的命令,也可以肯定还有其他您从未听说过的选项。

You’ll also definitely find commands you didn’t know existed. With so many different ways to search and track down information, it’s awesome to have it all at your fingertips.

您肯定还会找到您不知道的命令。 搜索和跟踪信息的方法多种多样,触手可及的一切真是太棒了。

翻译自: https://www.howtogeek.com/663440/how-to-use-linuxs-man-command-hidden-secrets-and-basics/

linux man命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值