命令分类
命令有四种类型
- 可执行程序。例如
find
命令,它路径为/usr/bin/find
。 - shell内置命令。例如
cd
命令。 - shell函数。
- 别名。例如在Ubuntu中,
ls
是ls --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
显示的信息
grep
需要使用到正则表达式,而info grep
里面正好包含了正则表达式的部分,把光标移动到Regular Expressions
上,然后按Enter键就可以查询正则表达式的信息。