Linux命令行大全:和命令打交道

在本章中,我们会进一步揭开命令的神秘面纱,甚至创建属于个人的命令。

  • type:显示命令类型。
  • which:显示可执行文件的位置。
  • help:获取Shell内建命令的帮助信息。
  • man:显示命令的手册页。
  • apropos:显示适合的命令清单。
  • whatis:显示手册页的简述。
  • info:显示命令的info条目。
  • alias:创建自己的命令。

5.1命令究竟是什么

符合下列4种情况之一的,都可以称为命令。

  • 就像我们在/ usr / bin中见到的那些文件一样。在这一分类中,程序可以是由C和C ++编写并进行编译生成的二进制文件,也可以是由Shell, Perl,Python,Ruby等脚本语言编写的脚本。
  • 在Shell中的内建命令。Bash支持大量内建命令,cd命令就是其中一个。
  • Shell函数。Shell函数是并入环境中的微型Shell脚本。在后文中,我们将介绍环境配置和Shell函数的编写,目前只需要知道其中的存在即可。
  • 别名。别名是我们在其他命令的基础上自己定义的命令。

5.2识别命令

知道使用的命令在某种情况下经常很有帮助,Linux操作系统提供了几种方法。

5.2.1 type-显示命令类型

type命令属于Shell内建命令,能够显示指定的命令属于哪种类型。其用法如下:

type_command_

其中,command是想要检查的文件名。下面是一些示例:

[me@linuxbox ~]$ type type
type is a shell builtin
[me@linuxbox ~]$ type ls
ls is aliased to 'ls --color=tty'
[me@linuxbox ~]$ type cp
cp is /bin/cp

从输出结果中可以看到,有3种不同的命令。注意ls(取自Fedora系统),它实际上是添加了--color=tty选项的ls命令的别名。现在我们终于知道了为什么ls的输出结果是彩色的!

5.2.2哪个显示显示文件的位置

有时候,系统中安装的程序不止一个版本。尽管这种情况在桌面系统中并不常见,但在大型服务器上却是司空见惯的。为了确定某个程序的确切位置,可以使用which命令:

[me@linuxbox ~]$ which ls
/bin/ls

which命令只适用于可执行文件,不适用于内建命令或代替实际可执行文件的别名。如果试图对Shell内建命令(例如cd命令)使用which命令,要么不会有任何输出结果,要么得到错误消息:

[me@linuxbox ~]$ which cd
/usr/bin/which: no cd in (/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games)

这算是“命令未找到”(command not found)的一种“精致”说法吧。

5.3 获取命令文档

知道了什么是命令之后,我们现在就可以获取各类命令可用的文档了。

5.3.1 help—获取Shell内建命令的帮助信息

Bash自带的帮助功能可用于所有Shell内建命令。只需要输入help,然后加上Shell内建命令的名称即可。下面是一个示例:

[me@linuxbox ~]$ help cd
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..

关于命令语法,有一点要注意:如果在命令语法描述中出现了方括号,表示这些项是可选的。|表示项与项之间是互斥关系。对于上面的cd命令:

cd [-L|[-P[-e]]] [dir]

该语法意思是,cd命令之后可以有选择地添加-L或-P选项,而且如果-P选项同时指定了-e选项,那么在其之后还可以跟上可选参数dir。

尽管 cd 命令的帮助文档简洁明了,但绝对算不上教程,我们可以看到,其中还提到了不少尚未提及的内容!别担心,后文中我们会讲到的。

5.3.2 --help—显示用法信息

很多程序都支持--help选项,该选项可以显示命令所支持的语法和选项的相关描述。例如:

[me@linuxbox ~]$ mkdir --help
Usage: mkdir [OPTION] DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
 
  -Z, --context=CONTEXT (SELinux) set security context to CONTEXT
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
      --help        display this help and exit
      --version     output version information and exit
Report bugs to <[email protected]>.

部分程序可能不支持--help选项,不过可以先试一试。这通常会产生一条错误消息,在其中也可以发现同样的用法信息。

5.3.3 man—显示命令的手册页

大多数用于命令行的程序会提供一份叫作手册(manual)或手册页(man page)的正式文档。有一个特殊的分页程序man可以浏览这种文档。其用法如下:

man program

其中,program是待浏览的手册页对应的命令名称。

手册页的格式各不相同,不过一般都会包含下列部分。

  • 标题(手册页的名称)。
  • 命令语法提要。
  • 命令作用描述。
  • 命名选项清单及其描述。

不过,手册页中通常并不包含示例,其目的是作为参考,并非教程。让我们尝试浏览一下ls命令的手册页:

[me@linuxbox ~]$ man ls

在大多数Linux系统中,man命令使用less命令显示手册页,所以在浏览的时候,熟悉的所有less命令都照样管用。

man命令显示的“手册页”被分为若干节(section),不仅涵盖用户命令,还包括系统管理命令、编程接口、文件格式等。表5-1描述了手册页的组织结构。

表5-1手册页的组织结构

内容

1

用户命令

2

系统调用的编程接口

3

C库函数的编程接口

4

特殊文件,例如设备异步和驱动程序

5

文件格式

6

游戏和娱乐,例如屏幕保护程序

7

杂项

8

系统管理命令

有时候我们需要参考手册页的某些节,从中查找所需的内容。当要查找的文件格式同时也是命令名称的时候,更方便。如果没有指定节号,则显示最先匹配到的那一对(可能会是第1节)。为了指明节号,可以像下面这样:

man_section search_____term_

例如:

[me@linuxbox ~]$ mkdir --help
Usage: mkdir [OPTION] DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
  -Z, --context=CONTEXT (SELinux) set security context to CONTEXT
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
      --help        display this help and exit
      --version     output version information and exit
Report bugs to <[email protected]>.

该命令会显示描述/ etc / passwd文件格式的手册页。

5.3.4 apropos-显示适合的命令清单

关于apropos命令可以根据关键字在手册页列表中搜索可能的匹配项。这种方法比较粗糙,但有时倒也管用。下面是用一个关键字partition搜索手册页的示例:

man_program_

输出结果中每行的第一个分区是手册页的名称,第二个分段是相应的节。注意,man命令的-k选项和apropos命令具有相同的功能。

5.3.5 whatis-显示手册页的简述

whatis命令会显示匹配指定关键字的手册页名称和单行描述:

[me@linuxbox ~]$ whatis ls
ls                   (1) - list directory contents

难以阅读的手册页

Linux和其他类UNIX系统提供的手册页本就是参考文档,并非学习教程。 ,我仔仔细细地翻看了Bash的手册页,确保涵盖了其中大部分主题。如果输出的话,密密麻麻的文字超过了80页,其采用的组织结构绝对让新手感到疑惑。

但是,手册页内容准确,用词简练,事无巨细地覆盖了方方面面。

5.3.6 info—显示程序的info清单

GNU项目为自家的程序提供了手册页的替代品:info。Info页使用名为info(恰如其分)的阅读器显示。info页中也包含超链接,和我们平时看到的网页颇为相像。下面是一个示例:

File: coreutils.info, Node: ls invocation, Next: dir invocation, Up:
Directory listing
10.1 'ls': List directory contents
==================================
The 'ls' program lists information about files (of any type, including
directories). Options and file arguments can be intermixed arbitrarily, as
usual.
   For non-option command-line arguments that are directories, by default 'ls'
lists the contents of directories, not recursively, and omitting files with
names beginning with '.'. For other non-option arguments, by default 'ls'
lists just the filename. If no non-option argument is specified, 'ls'
operates on the current directory, acting as if it had been invoked with a
single argument of '.'.
   By default, the output is sorted alphabetically, according to the
--zz-Info: (coreutils.info.gz)ls invocation, 63 lines --Top----------

info程序读取info文件,该文件按照树形结构组织成各个单独的节点,每个节点包含一个主题。info文件包含的超链接可以让你在节点之间跳转。超链接可以通过前置的星号来识别,将光标放在超链接上并按Enter键即可激活。

输入info和程序名称(可选)就可以启动info程序。表5-2描述了在显示info页时常用的控制命令。

表5-2 info命令

命令

操作

显示命令帮助

上翻页键(Page Up)或退格键(BackSpace)

显示上一页

下翻页键(Page Down)或空格键

显示下一页

n

显示下一个(next)节点

p

显示上一个(previous)节点

u

显示当前上游的父节点(up),通常是一个菜单

Enter键

进入光标所在的超链接

q

退出(quit)

到目前为止,我们讨论过的大部分命令行程序属于GNU项目的Coreutils软件包,输入下列命令:

[me@linuxbox ~]$ info coreutils

我们会看到一个菜单页面,其中包含指向Coreutils软件包内各个程序的超链接。

5.3.7 文档文件

系统中安装的很多软件包都有自己的文档文件,它们被存放在/usr/share/doc目录中。其中大部分文档文件采用的是纯文本格式,可以使用less命令来查看。有些文件采用的是HTML格式,可以用Web浏览器来查看。我们可能会碰到一些以.gz扩展名结尾的文件。这表明它们是经过gzip压缩过的。gzip软件包中有一个特殊版本的less命令,它叫作zless,可以显示由gzip压缩的文档文件的内容。

5.4 使用alias创建自己的命令

现在可以开始试着写程序了!我们将使用alias命令来创建自己的命令。但是在动手之前,我们需要展示一个命令行的小技巧:可以使用分号作为分隔符,在命令行中一次性输入多个命令。就像下面这样:

command1; command2; command3...

来看一个例子:

[me@linuxbox ~]$ cd / usr; ls; cd -
bin games include lib local sbin share src
/home/me
[me@linuxbox ~]$

可以看到,我们在一行中放置了3个命令。首先将当前工作目录更改为/usr,然后列出目录内容,最后返回先前的目录(使用cd-),这样就又回到了起点。现在,我们使用alias命令将上述命令序列变成一个新命令。第一件事就是先为新命令创建别名。让我们试一试test。不过在此之前,最好检查一下名称test是否已经被占用了。为此,使用type命令:

[me@linuxbox ~]$ type test
test is a shell builtin

test这个名称果然已经被占用了,那就试一试foo:

[me@linuxbox ~]$ type foo
bash: type: foo: not found

很好!foo可以用,让我们来创建别名吧:

[me@linuxbox ~]$ alias foo='cd /usr; ls; cd -'

注意alias命令的写法:

alias _name_='_string_'

在alias之后,我们指定了别名,紧接着(不允许出现空白字符[[1]])是等号,然后是单引号引用的字符串,包含着要赋给别名的内容。定义好的别名可以出现在Shell允许出现命令的任何地方。让我们来试一下 :


[me@linuxbox ~]$ foo
bin games include lib local sbin share src
/home/me
[me@linuxbox ~]$

我们可以使用type命令查看别名:

[me@linuxbox ~]$ type foo
foo is aliased to 'cd /usr; ls; cd -'

unalias命令可以删除别名:

[me@linuxbox ~]$ unalias foo
[me@linuxbox ~]$ type foo
bash: type: foo: not found

例如,前面讲到的ls是如何通过别名来添加颜色支持的:

[me@linuxbox ~]$ type ls
ls is aliased to 'ls --color=tty'

要想知道系统中定义的所有别名,使用不加任何参数的alias命令即可。下面是Fedora系统中默认定义好的一些别名。开动一下脑筋,想一想它们的作用:

[me@linuxbox ~]$ alias
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'

在命令行定义别名还有一个小问题。当Shell会话结束时,这些别名也会随之消失。在第11章中,我们将学习如何将别名添加到系统环境初始化文件中。现在,我们已经成功地向Shell编程世界迈出了一小步,尽情享受此刻吧!

5.5总结

我们已经学会了如何获取命令文档,不妨碍试着看一下之前介绍的所有命令的文档,研究这些命令的其他可用选项并动手实践!

本文摘自:《Linux命令行大全 第2版》

本书对Linux命令行进行详细的介绍,全书内容包括4个部分,第一部分由Shell的介绍开启命令行基础知识的学习之旅;第二部分讲述配置文件的编辑,如何通过命令行控制计算机;第三部分探讨常见的任务与必备工具;第四部分全面介绍Shell编程,读者可通过动手编写Shell脚本掌握Linux命令的应用,从而实现常见计算任务的自动化。通过阅读本书,读者将对Linux命令有更加深入的理解,并且可以将其应用到实际的工作中。
本书适合Linux初学人员、Linux系统管理人员及Linux爱好者阅读。

猜你喜欢

转载自blog.csdn.net/epubit17/article/details/114655894
今日推荐