Linux 命令行界面获取帮助信息

内容纲要

查看【Linux】专题可浏览更多内容

Linux 命令之多光靠死记硬背是不科学的,在遇到问题时除了使用互联网:

  • 搜索引擎
  • 官网文档
  • 社区论坛

操作系统也提供了一些命令帮助我们。

什么是命令

在获取帮助信息前需要先了解下什么是命令,命令一般分为几种类型:

  • 外部命令:编译生成的二进制文件或脚本语言编写的脚本;
  • 内建命令:Shell 的内建命令,与 Shell 于一体,不借助外部程序运行;
  • 别名:在其他命令的基础上自定义自己的命令;
  • Shell 函数:与别名相比可以封装多行命令以供使用;

查看命令类型

使用 type 命令查看目标命令是什么类型的:

type cd # cd is a shell builtin
type mkdir # mkdir is hashed (/usr/bin/mkdir)
type ls # ls is aliased to `ls --color=auto'

如上 mkdir 就是一个外部命令,值得一提的另一个命令 whcih 可以用来找到可执行文件的具体位置,如:

which mkdir
/usr/bin/mkdir

which 并不能用来查找内建命令,前面说了内建命令实际上是 Shell 自身提供的命令,试试 which cd

另外,如上示例的 ls 命令是 ls --color=auto 的别名(所以它的输出结果才是彩色的),那它到底是外部命令还是内建命令呢?可以使用 -a选项:

type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls

除了 -a 还有一个 -t 选项可以直接了当的告诉我们这是一个什么类型的命令:

type -t cd # alias
type -t mkdir # file
type -t ls # alias

最后,有的命令即有内建命令也有外部命令,例如 pwd

获取帮助信息

man

大多数用于命令行的程序会提供一份叫作手册(manual)或手册页(man page)的正式文档。

使用 man 命令可以浏览这些文档,例如查看 man 命令自身的文档:

man man

man 命令实际上是使用 less 命令显示手册信息,所以 less 命令的快捷键是可以照用的:

  • 显示帮助:h
  • 向上一行:方向键上
  • 向下一行:方向键下
  • 向前翻页:bPage Up
  • 向后翻页:空格键Page Down
  • 跳至页首:g
  • 跳至页尾:G
  • 搜索关键词:/关键词
  • 重复上一次搜索:n
  • 退出:q

此处因为有大小写的运用所以区分大小写,之后为了美观会将按钮图标都写成大写。

MAN 分为 9 个节(section),具体如下:

#名称
1Executable programs or shell commands
可执行程序或 shell 命令
2System calls (functions provided by the kernel)
系统调用(内核提供的函数)
3Library calls (functions within program libraries)
系统调用(内核提供的函数)
4Special files (usually found in /dev)
特殊文件(通常位于 /dev)
5File formats and conventions eg /etc/passwd
文件格式和规范,如 /etc/passwd
6Games
游戏
7Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
杂项(包括宏包和规范)
8System administration commands (usually only for root)
系统管理命令(通常针对 root 用户)
9Kernel routines [Non standard]
内核标准[非标准]

因为有时候命令、系统文件调用可能会有重名的情况,所以要加上章节进行区分。例如使用命令 man man 后可以看到左上角有个「MAN(1) 」这表示这是 man 的第一个篇章。

以修改密码的命令 passwd 为例,它既是命令也是配置文件(/etc/passwd),所以 1 和 5 节都有关于它的内容,加上章节后就可以看到各自的帮助信息。

# 查看 passwd 命令的帮助信息
man passwd # 或者 man 1 passwd

# 查看 /etc/passwd 文件的帮助信息
man 5 passwd

如果你只知道「passwd」这么一个命令名,但是不确定其是命令、文件还是其他如库调用,那么可以使用 -a 选项带上参数也就是要查找的命令名:

man -a passwd

在打开的帮助信息的左上角显示的是「PASSWD(1)」则代表第一章,如果不是想要的结果可以按 Q 退出后看到提示:

--Man-- next: passwd(1ssl) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]

MAN 会找到相关信息,你可以:

  • 如果就是你要的信息按 Enter 回车键查看;
  • Ctrl + D 查看下一条相关信息;
  • 都没有想要的按 Ctrl + C 退出;

也可以使用 -k 选项加上关键词获取可能相关的信息:

? man -k 和使用 apropos 命令是相同效果。

man -k passwd

# 等同于
apropos passwd

返回:

chgpasswd (8)        - update group passwords in batch mode
chpasswd (8)         - update passwords in batch mode
gpasswd (1)          - administer /etc/group and /etc/gshadow
grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB
openssl-passwd (1ssl) - compute password hashes
pam_localuser (8)    - require users to be listed in /etc/passwd
passwd (1)           - change user password
passwd (1ssl)        - compute password hashes
passwd (5)           - the password file
update-passwd (8)    - safely update /etc/passwd, /etc/shadow and /etc/group

MAN 的定位在于参考手册,内容准确、用词简练,事无巨细地涵盖了方方面面。庞大的阅读量可能会让新手感到困惑和头大,如果可以还是建议耐心阅读。

但有的时候只是想快速了解命令用途和用法,并没有太多时间阅读手册,也是有其他帮助信息的,不必担心。

whatis

whatis 命令可以一句话描述命令的用途:

whatis pwd
pwd (1)  - print name of current/working directory

help

对于 Shell 的内建命令可以使用 help 命令来获取帮助信息,例如 cd 命令就是一个内建命令(a shell builtin)。

help pwd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.

    Options:
      -L        force symbolic links to be followed: resolve symbolic
                links in DIR after processing instances of `..'
      -P        use the physical directory structure without following
                symbolic links: resolve symbolic links in DIR before
                processing instances of `..'
      -e        if the -P option is supplied, and the current working
                directory cannot be determined successfully, exit with
                a non-zero status
      -@        on systems that support it, present a file with extended
                attributes as a directory containing the file attributes

    The default is to follow symbolic links, as if `-L' were specified.
    `..' is processed by removing the immediately previous pathname component
    back to a slash or the beginning of DIR.

    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

如上,help 命令告诉我们 cd 命令的作用以及其选项和选项的作用。

注意第一行,它告诉了我们选项的搭配用法,有的命令选项可能是不能一起搭配使用的,就会显示 | 这个分隔符的意思是「或」,如:command [-A|-B] 表示可以使用选项 -A-B,但不能一起使用如:-AB

--help

大部分命令还可以使用 --help 来获取帮助信息:

mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

info

info 命令是用于阅读 info 格式文档的帮助命令,info 比 man 相对友好容易理解,但讽刺的是学习和使用 info 又相对困难。

类似于使用 man man 查看 man 的帮助信息,你也可以使用 info 查看 info 的帮助信息。

info info
Next: Stand-alone Info,  Up: (dir)

Stand-alone GNU Info
********************

This documentation describes the stand-alone Info reader which you can
use to read Info documentation.

   If you are new to the Info reader, then you can get started by typing
'H' for a list of basic key bindings.  You can read through the rest of
this manual by typing <SPC> and <DEL> (or <Space> and <Backspace>) to
move forwards and backwards in it.

* Menu:

* Stand-alone Info::            What is Info?
* Invoking Info::               Options you can pass on the command line.
* Cursor Commands::             Commands which move the cursor within a node.
* Scrolling Commands::          Commands for reading the text within a node.
* Node Commands::               Commands for selecting a new node.
* Searching Commands::          Commands for searching an Info file.
* Index Commands::              Commands for looking up in indices.
-----Info: (info-stnd)Top, 31 lines --Top------------------------------------------------------
Welcome to Info version 6.7.  Type H for help, h for tutorial.

info 会读取 info 文件,该文件按照树形结构组织成各个单独的节点,每个节点包含一个主题。

info 文件包含的超链接可以在节点之间跳转。超链接可以通过前置的星号来识别,将光标放在超链接上并按 Enter 键即可跳转。

info 的实用快捷键:

  • 显示帮助:?
  • 上一页:BackSpace删除键 或 Page Up
  • 下一页:空格键Page Down
  • 上一个节点:P
  • 下一个阶段:N
  • 显示当前节点的父节点:U
  • 进入光标所在的超链接(有下划线的文字):Enter 回车键;
  • 退出:Q

文档文件

除此之外,许多软件包的各自文档你还可以在 /usr/share/doc 目录中找到。

在该目录中:

  • 大多数都是文本文件,可以使用 less 命令进行查看,如:less README,具体快捷键可以查看前文所述 man 命令时所讲到的快捷键;
  • 有一些 HTML 文件则需要使用浏览器打开;
  • 还有的是一些是 .gz 结尾的 gzip 软件包格式,可以使用less 特别版命令 zless 进行查看,如:zless changelog.gz