Linux入门真经-009whatis、which、alias、whereis
whatis命令
上一节我们提到过man是有分章节的,不通的章节介绍的侧重点不同,有时一个命令可能会在多个章节下说明
那么我们如何知道某一个命令的man手册处于哪个章节呢?
可以使用whatis命令。
如下图,whatis crontab的输出表示,crontab命令在man1和man5中都有说明,你要查命令用法可以使用man 1 crontab,查配置文件用法可以使用man 5 crontab;若你使用的是man crontab,显示的是man1的内容,man5中的内容则不予显示。(你暂时不用管这个命令是干什么的,我们现在只是在介绍whatis,当然,感兴趣的话,你也可以自己先man一下)
which命令
上一节我们同样提到过,命令有内部命令和外部命令之分。外部命令具有独立的可执行文件,那么,外部命令的可执行命令在哪里呢
通过which命令可以查到命令所对应的二进制文件:
which [options] programname [...]
举例:
[[email protected] ~]# which cd
/usr/bin/cd
[[email protected] ~]#
which命令的输出告诉我们:cd命令位于/usr/bin/cd下。
你可能会发现,有些程序的输出有些不同,比如:
[[email protected] ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
这是因为ls使用了alias别名机制
命令别名
命令可以有别名;别名可以与原名相同,此时原名被隐藏;此时如果要运行原命令,则使用\COMMAND,如(ls -a的作用是列出当前目录下的所有文件,你应该能发现,直接ls显示出的列表是有颜色的,而原生的ls命令其实是不带颜色的):
获取所有可用别名(我们看到alias ls='ls --color=auto'这一定义,也就是说,我们输入ls时,实际执行的是ls --color=auto):
[[email protected] /]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which--tty-only --read-alias --show-dot --show-tilde'
[[email protected] /]#
定义别名:
~]#alias NAME='COMMAND'
如,定义一个命令别名快速切换到/etc/sysconfig/network-scripts/
[[email protected] ~]# alias cdnet='cd /etc/sysconfig/network-scripts/'
[[email protected] ~]# cdnet
[[email protected] network-scripts]# pwd
/etc/sysconfig/network-scripts
注意:仅对当前shell进程有效,永久生效需要修改配置文件,以后学会修改文本文件后会详说。
撤销别名:
~]# unalias NAME
如,撤销unlias别名之后,该别名就无法再使用:
[[email protected] network-scripts]# unalias cdnet
[[email protected] network-scripts]# cdnet
-bash: cdnet: command not found
[[email protected] network-scripts]#
echo与PATH
大家是否有想过,外部命令的二进制文件都存放在哪里呢?我们输入命令时,并没有直接指出命令对应二进制程序在系统中的位置,系统是如何能够找到这些文件的呢。
其实,shell中包含了一个名为PATH的变量,PATH中记录了命令常用的存放路径,当我们输入命令名的时候,他就会在PATH中包含的路径中依次查找。(变量会在后面的教程中详述,这里先混个眼熟)
echo命令可以显示变量,显示变量的值时需要在变量前加上$符号。如下:
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]#
冒号为分隔符,我们可以看到,系统自带命令默认是存放在这几个路径,其中:
/bin, /usr/bin中存放的命令普通用户均可执行
/sbin, /usr/sbin中的命令只有管理员可执行
/root/bin下的命令为root用户所独有
whereis
whereis命令可以同时列出二进制命令文件的所处位置以及对应的man手册
[[email protected] ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[[email protected] ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz
下一节我们会继续介绍linux的常用命令。