linux-常用命令-帮助命令


帮助命令

一、Linux 帮助命令简介

Linux 系统的命令数量有上千个,每个命令又有若干个甚至数十个参数指出不同情景下的使用。

有一些命令是我们日常工作需要经常使用的,即便不是特意背命令,也会因熟能生巧而印象深刻。但是对于那些不熟悉的命令,或者是熟悉命令的不熟悉的参数呢?

当然,我们不需要耗费大量精力去记忆这些命令和参数,只需要正确使用Linux 的帮助命令,就能够快速地定位到自己想要的命令和参数。

Linux 的帮助命令主要包含三个:

1. help命令 与 --help 参数
2. man
3. info

二、help命令与–help参数

2.1 help命令

help 命令能在控制台打印出对应命令的帮助信息,使用方式为:

help <command>

例如,我们要查看 cd 命令和 mv 命令的帮助命令,使用上述形式的 help 命令时能够得到如下输出:

ligendeMacBook-Pro:regan-note ligen$ help cd
cd: cd [-L|-P] [dir]
    Change the current directory to DIR.  The variable $HOME is the
    default DIR.  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, i.e. `.'.  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, then try the word as a variable
    name.  If that variable has a value, then cd to the value of that
    variable.  The -P option says to use the physical directory structure
    instead of following symbolic links; the -L option forces symbolic links
    to be followed.
ligendeMacBook-Pro:regan-note ligen$ help mv
bash: help: no help topics match `mv'.  Try `help help' or `man -k mv' or `info mv'.

为什么会出现上述差异?为什么不能查看 mv 命令的帮助信息?这里需要引入一个“内建命令”和“外部命令”的概念

2.2 内剪命令和外部命令

Linux 的内建命令是 shell 程序的一部分,Linux 系统加载运行时就被加载并驻留在系统内存里的,因此执行速度较快。

Linux 的外部命令是通过额外安装获得的命令,不随系统一起被加载到内容中,运行速度慢但功能强大。

使用 type 命令可以查看该命令是内建命令还是外部命令

type <command> 

例如上文中的 cd 和 mv命令 我们可以执行得出

cd is a shell builtin (cd 是一个内置的 shell)

ligendeMacBook-Pro:regan-note ligen$ type cd
cd is a shell builtin

mv 是一个外部命令

ligendeMacBook-Pro:regan-note ligen$ type mv
mv is hashed (/bin/mv)

2.3 –help 参数

若是想用 help 来查询外部命令的帮助文档,上述形式是行不通的,但是可以用下述形式查询外部命令的帮助文档

mv --help

三、man

man 是 manual 的简写,与 help 命令和 –help 参数不同,使用 man 命令查询帮助手册时会进入 man page 界面,而非直接打印在控制台上。同时,相比与 help,man 命令的信息更全,help 则显示的信息简洁

man [option] [command]

man mv

输出

MV(1)                     BSD General Commands Manual                    MV(1)

NAME
     mv — move files

SYNOPSIS
     mv [-f | -i | -n] [-v] source target
     mv [-f | -i | -n] [-v] source ... directory

DESCRIPTION
     In its first form, the mv utility renames the file named by the source operand to the destination path named by the target operand.  This form is assumed when the last op‐
     erand does not name an already existing directory.

     In its second form, mv moves each file named by a source operand to a destination file in the existing directory named by the directory operand.  The destination path for
     each operand is the pathname produced by the concatenation of the last operand, a slash, and the final pathname component of the named file.

     The following options are available:

     -f      Do not prompt for confirmation before overwriting the destination path.  (The -f option overrides any previous -i or -n options.)
:

我们注意到,在第一行有 “MV(1)” 的字样,括号内的数字代表什么含义呢?它其实是对所查询信息的一个分类。

数字 意义
1 用户在shell环境中可操作的标准命令或可执行文件
2 系统内核调用的函数及工具
3 常用的库函数
4 设备文件与设备说明等
5 配置文件或文件格式
6 游戏等娱乐
7 协议信息等
8 系统管理员可用的管理命令
9 与 Linux 内核相关的文件文档

备注:英文不好的同学,可以安装man 中文包

mac 可以使用homebrew 安装groff ,再配置man.conf

效果如下:

HUTDOWN(8)           Linux System Administrator's Manual          SHUTDOWN(8)

NAME
       shutdown - 关闭系统

总览 SYNOPSIS
       /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message]

描述 DESCRIPTION
       shutdown
       以一种安全的方式关闭系统。所有登陆用户都可以看到关机信息提示,并且  lo‐
       gin(1)
       将被阻塞。可以指定立刻关机,也可以指定系统在一定的延时后关机。所有进程都将接收到
       SIGTERM                       信号。这可以使                      vi(1)
       等程序有时间将处于编辑状态的文件进行存储,邮件和新闻程序进程则可以将所有缓冲池内的数据进行适当的清除等等。
       shutdown   通过通知   init  进程,要求它改换运行级别来实现。运行级别  0
       用来关闭系统,运行级别        6        用来重启系统,运行级别         1
       用来使系统进入执行系统管理任务状态,如果没有给出  -h 或 -r 标志时,这是
       shutdown           命令的默认工作状态。具体关机或重启所执行的操作请查阅
       /etc/inittab 文件中相应的运行级别栏。

选项 OPTIONS
       -a     使用 /etc/shutdown.allow 来验证身份。

       -t sec 通知    init(8)    在转换到其它运行级别前,发送警告    (warning)
              信号后延时 (sec) 秒数后再发送关闭 (kill) 信号。

       -k     并非真正关机,只向所有人显示警告信息。

       -r     重启。

       -h     停机。

       -n     [DEPRECATED(不应再使用)]             不调用              init(8)
              程序进行关机操作,而由自己进行。不建议用户使用这种关机方式,它的结果一般也不是你希望的那样。

       -f     重启时跳过磁盘检测。

       -F     重启时强制磁盘检测。

       -c     取消运行中的     shutdown     进程。不可能为此选项指定      time
              参数,但你可以在命令行输入一条解释消息来向所有用户说明。(一般的shut‐
              down指令可以用按“+”号来进行中断)

       time   关机时间。

四、info命令

info cd

info 命令的功能基本与 man 命令相似,能够显示出命令的相关资料和信息。

而与 man 命令稍有区别的是,一方面,info 命令可以获取所查询命令相关的更丰富的帮助信息;另一方面,info page 将文件数据进行段落拆分,并以 “节点” 的形式支撑整个页面框架,并将拆分的段落与节点对应,使得用户可以在节点间跳转而方便阅读每一个段落的内容。

发表评论