update at 2017年 12月 19日 星期二 22:39:52 CST

This commit is contained in:
darksun 2017-12-19 22:39:52 +08:00
parent 839e207fec
commit 16583e9bf8
2 changed files with 168 additions and 169 deletions

View File

@ -1,169 +0,0 @@
translating by lujun9972
Learn To Use Man Pages Efficiently
======
A while ago, we have published a brief guide that described how to easily [**recall forgotten Linux commands**][1]. That guide was really quite useful for those having trouble at remembering commands. Today, we are going to learn how to get want you want from man pages effectively and quickly. As you might already know, a typical man page is divided into several parts, each with a distinct heading. You might need to scroll down for quite a long time when you're looking for a particular information on the specific flag/option. This is really inefficient and time consuming process. This is why it is important to learn to use man pages efficiently to find out what exactly you want to know.
In this brief tutorial, I will be sharing few important tricks which I use daily when referring man pages.
### Learn To Use Man Pages Efficiently
#### Basic use
As we all know, we can open a man page of a command, for example "mkdir", using command:
```
man mkdir
```
Use **spacebar** , **d** , **b** and **up** / **down** arrows to browse through the man page. To go to the end of the man page, press **End** key and to go to the fist page of a man page, press **Home** key. Press **h** key in the currently opened man page to know all useful keyboard shortcuts and general usage information.
[![][2]][3]
Press **q** to exit the man page.
#### Recall a forgotten command
For those who don't know which command they actually want can refer the link that I attached in the first paragraph of this guide. We also can do it using man pages too. Let us say, you want to create a directory, but you couldn't remember what command we use to create a directory.
To do so, use grep command with man:
```
man -k directory | grep create
```
Sample output would be:
```
CURLOPT_NEW_DIRECTORY_PERMS (3) - permissions for remotely created directories
libssh2_sftp_mkdir_ex (3) - create a directory on the remote file system
mkdir (2) - create a directory
mkdirat (2) - create a directory
mkdtemp (3) - create a unique temporary directory
mkdtemp (3p) - create a unique directory or file
mkfontdir (1) - create an index of X font files in a directory
mklost+found (8) - create a lost+found directory on a mounted Linux second extended file...
mkstemp (3p) - create a unique directory
mktemp (1) - create a temporary file or directory
pam_mkhomedir (8) - PAM module to create users home directory
```
[![][2]][4]
Just read the description of the each command and pick the suitable command. Ahh, you now remember. **mkdir** is the one that you 're looking for, isn't it? It's that simple.
#### Search within man pages
Once you're in a man page, you may want to look for specific string. To do so, just type **/** (forward slash) followed by your search string like below
```
/<search_string> or <pattern>
```
Let us say, you're in man page of mount command and wants to look for information on the **- bind** option. To do so, type:
```
/bind
```
[![][2]][5]
Any matches to the search string in the current man page will be highlighted.
[![][2]][6]
Press **" n"** and **" SHIFT+n"** to browse through the next and previous matches.
/pattern(or string) - will search forward for (N-th) matching line. You can also do the backward searching using **?pattern**. This can be helpful if you are at the end or middle of the man page.
```
?bind
```
To display only matching lines, type:
```
&bind
```
[![][2]][7]
In this search method, you don't have to use "n" or "shift+n" to scroll through next and previous matches. **& pattern** will only display the matching lines that contains the search term, everything else will be omitted.
#### Search matches without opening man page
It is also possible to search for information on the specific option without opening man page.
For instance, you're looking for information on the **-m** option of **mkdir** command. To find it out, run:
```
man mkdir | grep -e '-m'
```
Or,
```
man mkdir | grep -- '-m'
```
[![][2]][8]
This command will display the **first occurrence** of **-m** in the man page of mkdir command. As we see in the above command -m represents MODE (chmod).
If you want to see the complete man page of mkdir command but skip directly to the first occurrence of **-m** , use the following command:
```
man mkdir | less +/-m
```
[![][2]][9]
Here is another example:
```
man mount | less +/--bind
```
[![][2]][10]
Press "n" and "SHIFT+n" to browse through the next and previous matches.
Also read:[3 Good Alternatives To Man Pages Every Linux User Should Know][11]
#### Export entire man page to text file
We can export the entire man page of a specific command to a text file. To do so, just run the following command:
```
man mount > mount.txt
```
This command will export the man page of the mount command to mount.txt file and save it in the current working directory.
It is also possible to obtain a simpler version of a man page, without backspaces and underscores, using the following command.
```
man mount | col -b > mount.txt
```
To know more details about man pages, run:
```
man man
```
This command will display the man page about the man pages. These tricks are just basics but enough to get going. These tricks will save you a lot of time and avoid unlimited scrolling.
And, that's all for today. Hope you find this useful. More good stuffs to come. Stay tuned!
Cheers!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/learn-use-man-pages-efficiently/
作者:[][a]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.ostechnix.com
[1]:https://www.ostechnix.com/easily-recall-forgotten-linux-commands/
[2]:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[3]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-4.png ()
[4]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-3.png ()
[5]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-5.png ()
[6]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-6.png ()
[7]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-8.png ()
[8]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-1.png ()
[9]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-2-1.png ()
[10]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-7.png ()
[11]:https://www.ostechnix.com/3-good-alternatives-man-pages-every-linux-user-know/

View File

@ -0,0 +1,168 @@
学习如何高效地使用 Man 页
======
不久前,我们发布了一篇简短的指引描述了如何轻易地[回忆起忘记的 Linux 命令 ][1]。那篇指引对于无法记住命令的人来说真的非常有用。今天,我们就来学习一下如何高效而又迅速第从 man 页中获取你所需要的信息。如你所知,一个标准的 man 页分成很多个部分,每部分都有一个独立的标题。当你想查看特定的标志/选项时,可能需要向下滚动很长时间才能找到。这这是个效率底下而且很耗时间的过程。这也是为什么学会高效使用 man 页来精确定位你想要的内容非常的重要。
在本文中,我会分享一些常用的跟 man 页相关的重要技巧。
### 学习高效地使用 Man 页
#### 基础用法
我们都知道,我们可以使用类似下面的命令来打开关于某个命令(比如 “mkdir”) 的 man 页:
```
man mkdir
```
可以使用 **spacebar****d****b** 以及 **up** / **down** 箭头来浏览 man 页。要跳转道 man 页的末尾,可以按 **End** 键而想跳转到 man 页的头部则可以按 **Home** 键。在当前打开的 man 页中按下 **h** 键会显示所有有用的键盘快捷键和一般用法。
[![][2]][3]
**q** 可以退出 man 页。
#### 回忆起忘记的命令
对于那些不知道想要哪个命令的家伙,可以去查看一下我第一段中提到的那个链接。使用 man 页我们也能做到这一点。假设说,你想要创建一个目录,而你忘记了使用哪个命令来创建目录。
为了回忆起那个忘记的命令,可以将 man 和 grep 命令联用:
```
man -k directory | grep create
```
输出结果为:
```
CURLOPT_NEW_DIRECTORY_PERMS (3) - permissions for remotely created directories
libssh2_sftp_mkdir_ex (3) - create a directory on the remote file system
mkdir (2) - create a directory
mkdirat (2) - create a directory
mkdtemp (3) - create a unique temporary directory
mkdtemp (3p) - create a unique directory or file
mkfontdir (1) - create an index of X font files in a directory
mklost+found (8) - create a lost+found directory on a mounted Linux second extended file。。。
mkstemp (3p) - create a unique directory
mktemp (1) - create a temporary file or directory
pam_mkhomedir (8) - PAM module to create users home directory
```
[![][2]][4]
你只需要阅读一下每个命令的描述然后挑选出合适的命令就行了。啊,现在你记起来了。**mkdir** 正式你想要的,对吧?就是那么简单。
#### 在 man 页中搜索
若你在 man 页中想要查找特定字符串。只需要输入 **/** (前斜线) 再加上你想要搜索的字符串,像这样
```
/<search_string> or <pattern>
```
假设你正在查看 mount 命令的 man 页,想要寻找关于 **- bind** 选项的相关信息。可以输入:
```
/bind
```
[![][2]][5]
当前 man 页中任何匹配搜索字符串的内容都会被高亮显示。
[![][2]][6]
按下 **"n"** 和 **"SHIFT+n"** 来查看下一个/上一个匹配的地方。
/模式(或者说字符串) - 会向前搜索匹配行。你也可以使用 **pattern** 进行向后搜索。这当你在 man 页的末尾或中间位置时非常有用。
```
bind
```
若想只显示匹配行,输入:
```
&bind
```
[![][2]][7]
使用这种方法,你无需使用 "n" 和 "shift+n" 来滚动道下一个/上一个匹配的位置。**& pattern** 只会显示那些包含搜索内容的行,其他的内容全都被省略掉。
#### Search matches without opening man page
也可以在不打开 man 页的前提下搜索指定选项的信息。
比如,你想了解 **mkdir** 命令中的 **-m** 选项的相关信息。可以运行:
```
man mkdir | grep -e '-m'
```
或者,
```
man mkdir | grep -- '-m'
```
[![][2]][8]
这个命令会显示出 mkdir 命令 man 页中 **第一次出现** **-m** 时的内容。从上面命令中我们可以看到 -m 表示的是 MODE (chmod)。
如果你想阅读 mkdir 命令的完整 man 页,但是要跳过第一次出现 **-m** 之前的内容,可以使用下面命令:
```
man mkdir | less +/-m
```
[![][2]][9]
这是另一个例子:
```
man mount | less +/--bind
```
[![][2]][10]
按下 "n" 或 "SHIFT+n" 可以浏览下一个/上一个匹配的位置。
参考阅读 :[3 Good Alternatives To Man Pages Every Linux User Should Know][11]
#### 将完整的 man 页导出道文本文件中
我们可以将指定命令的完整 man 页导出成文本文件。方法是运行下面命令:
```
man mount > mount.txt
```
该命令会将 mount 命令的 man 页导出到当前目录的 mount.txt 文件中。
也可以获取一个简化版的 man 页,没有退格和下划线,方法是使用下面命令。
```
man mount | col -b > mount.txt
```
要了解更多关于 man 页的详细信息,运行:
```
man man
```
该命令会显示出关于 man 页的 man 页。这些技巧都很基础但很实用。它们会节省你很多的时间而且能免去很多的滚动操作。
今天的内容就到这了。希望对你有帮助。更多好文即将到来。准备好哦!
Cheers
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/learn-use-man-pages-efficiently/
作者:[][a]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.ostechnix.com
[1]:https://www.ostechnix.com/easily-recall-forgotten-linux-commands/
[2]:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[3]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-4.png ()
[4]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-3.png ()
[5]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-5.png ()
[6]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-6.png ()
[7]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-8.png ()
[8]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-1.png ()
[9]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-2-1.png ()
[10]:http://www.ostechnix.com/wp-content/uploads/2017/12/man-pages-7.png ()
[11]:https://www.ostechnix.com/3-good-alternatives-man-pages-every-linux-user-know/