了解 Linux 命令的信息

命令分类

命令有四种类型

  1. 可执行程序。例如find命令,它路径为/usr/bin/find
  2. shell内置命令。例如cd命令。
  3. shell函数。
  4. 别名。例如在Ubuntu中,lsls --color=auto的别名。

识别命令

命令有四种分类,有时候需要知道命令的类型而做出不同的动作,例如要查询命令的信息,首先需要知道命令的类型,然后再调用不同工具命令来查询该命令的信息。

type - 显示命令的类型

type命令用来显示命令的类型

$ type find
find is /usr/bin/find

$ type cd
cd is a shell builtin

$ type git-branch-name 
git-branch-name is a function
git-branch-name () 
{ 
    git symbolic-ref HEAD 2> /dev/null | cut -d"/" -f 3
}

$ type ls
ls is aliased to `ls --color=auto'

type命令检测了这四个命令的类型,从结果可以看出,find命令的类型为可执行文件,cd命令的类型为shell内置命令,git-branch-name为shell函数,ls命令是ls --color=auto的别名。

which - 显示可执行程序命令的位置

对于可执行程序这种类型的命令,有时候需要知道它的确切位置,因为可能有几个不同版本。which命令就是用来显示可执行程序命令的位置,而对于shell内置命令以及别名,which命令就不适用。

$ which find
/usr/bin/find

其实,用type命令在识别可执行程序命令的时候,就已经显示了它的位置

$ type find
find is /usr/bin/find

获取命令文档

使用type命令获取命令的类型后,可以调用不同的文档查询命令来获取该命令的文档。

help - 获取shell内置命令的文档

cd是一个shell内置命令,通过help cd可以查询它的文档。

–help - 显示可可执行程序命令的使用信息

很多可执行程序命令支持--help选项来展示命令如何使用。

例如,我在使用cp命令的时候遇到一些困难,我想知道cp命令是否支持某些选项来解决我的困难,而cp命令又是一个可执行程序命令,因此我用cp --help来查询它的使用信息

man - 显示可执行程序命令的使用手册

用于命令的大部分可执行程序,提供了使用手册,可以使用man命令查询使用手册。

man ls可以展示ls命令的使用手册,它包含了很多部分,默认展示第一部分内容,也就是ls --help所显示的使用文档。

man命令还有一个很实用,但是又很少用到的功能。当我们知道有一个命令可以完成我们的工作,但是又不记得这个命令的名字,因此可以搜索使用手册,看哪些命令可以匹配我搜索的字符。

例如,现在我想查询搜索文件的命令

$ man -k "search.*files"
bzegrep (1)          - search possibly bzip2 compressed files for a regular expression
bzfgrep (1)          - search possibly bzip2 compressed files for a regular expression
bzgrep (1)           - search possibly bzip2 compressed files for a regular expression
find (1)             - search for files in a directory hierarchy
lzegrep (1)          - search compressed files for a regular expression
lzfgrep (1)          - search compressed files for a regular expression
lzgrep (1)           - search compressed files for a regular expression
xzegrep (1)          - search compressed files for a regular expression
xzfgrep (1)          - search compressed files for a regular expression
xzgrep (1)           - search compressed files for a regular expression
zegrep (1)           - search possibly compressed files for a regular expression
zfgrep (1)           - search possibly compressed files for a regular expression
zgrep (1)            - search possibly compressed files for a regular expression
zipgrep (1)          - search files in a ZIP archive for lines matching a pattern

man-k选项表明在手册页中搜索哪个字符串,我这里使用正则表达式来匹配搜索,因此搜索出这么多命令。

info - 显示程序的信息条目

man命令提供的可执行程序的使用手册,只是对命令的使用做了简单的描述。而GNU项目提供了一个替代物,info命令,它可以详细显示命令的各种信息。info命令会列出一些内容菜单,这些菜单像超链接一样,可以点击Enter键进入链接的内容。

以下为执行info grep显示的信息

info_grep

grep需要使用到正则表达式,而info grep里面正好包含了正则表达式的部分,把光标移动到Regular Expressions上,然后按Enter键就可以查询正则表达式的信息。

发布了44 篇原创文章 · 获赞 30 · 访问量 400万+

猜你喜欢

转载自blog.csdn.net/zwlove5280/article/details/103712095
今日推荐