乍一看这个标题有点拗口,什么叫对“命令”操作的命令呢?其实从字面上就可以理解我要表达的意思,今天这篇博客写的还是命令,但是这些命令有点不一样,不一样的地方在于这些命令操作的对象就是“命令”。
1, type,type可以查看命令是属于shell的内建命令还是外部命令
[root@node1 ~]# type cd cd is a shell builtin [root@node1 ~]# type ls ls is aliased to `ls --color=auto' [root@node1 ~]# type cd cd is a shell builtin [root@node1 ~]# type vi vi is /bin/vi
type后可以跟-a选项
[root@node1 /]# type -a pwd pwd is a shell builtin pwd is /bin/pwd
2, whereis,在linux中一切皆文件,命令也不例外,whereis就可以帮你定位这些“命令”文件的为位置和帮助文档
[root@node1~]# whereis vi vi:/bin/vi /usr/share/man/man1/vi.1.gz /usr/share/man/man1p/vi.1p.gz [root@node1~]# whereis cd cd:/usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz [root@node1~]# whereis ls ls: /bin/ls/usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
3, which,which的原意是哪一个,而linux中which可以列出命令的别名,命令的位置,以及命令相关的man文档的位置
root@node1 ~]# which ls alias ls='ls --color=auto' /bin/ls [root@node1 ~]# which cd /usr/bin/which: no cd in(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) [root@node1 ~]# which vi /bin/vi [root@node1 ~]# whereis vi vi: /bin/vi /usr/share/man/man1/vi.1.gz/usr/share/man/man1p/vi.1p.gz
4, alias,给命令制定一个别名,用户可以通过alias对命令做一些贴近个人习惯的定制
[root@node1~]# alias la='ls -al' [root@node1~]# la total59428 dr-xr-x---.22 root root 4096 Jun 6 14:15 . dr-xr-xr-x.25 root root 4096 Jun 7 08:34 .. drwxr-xr-x. 2 root root 4096 May 6 08:44 .abrt -rw-------. 1 root root 1592 May 7 04:40 anaconda-ks.cfg -rw-------. 1 root root 534 Jun 6 14:16 .bash_history -rw-r--r--. 1 root root 18 May 20 2009 .bash_logout -rw-r--r--. 1 root root 176 May 20 2009 .bash_profile -rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc drwxr-xr-x. 2 root root 4096 May 6 08:46 .cache drwx------. 4 root root 4096 May 6 08:44 .config -rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc drwx------. 3 root root 4096 May 6 08:44 .dbus drwxr-xr-x. 2 root root 4096 May 6 08:44 Desktop drwxr-xr-x. 2 root root 4096 May 6 08:44 Documents drwxr-xr-x. 2 root root 4096 May 6 08:44 Downloads -rw-------. 1 root root 16 May 6 08:44 .esd_auth drwx------. 4 root root 4096 May 6 20:53 .gconf drwx------. 2 root root 4096 May 6 20:55 .gconfd drwx------. 5 root root 4096 May 6 08:44 .gnome2 -rw-r--r--. 1 root root 107 May 6 20:53 .gtk-bookmarks drwx------. 2 root root 4096 May 6 08:44 .gvfs -rw-------. 1 root root 620 May 6 20:53 .ICEauthority -rw-r--r--. 1 root root 623 May 6 20:53 .imsettings.log -rw-r--r--. 1 root root 48228 May 7 04:40 install.log -rw-r--r--. 1 root root 9360 May 7 04:37 install.log.syslog drwxr-xr-x. 3 root root 4096 May 6 08:44 .local drwxr-xr-x. 2 root root 4096 May 6 08:44 Music drwxr-xr-x. 2 root root 4096 May 6 08:44 .nautilus drwxr-xr-x. 2 root root 4096 May 6 08:44 Pictures drwxr-xr-x. 2 root root 4096 May 6 08:44 Public drwx------. 2 root root 4096 May 6 08:44 .pulse -rw-------. 1 root root 256 May 6 08:44 .pulse-cookie -rw-------. 1 root root 218 May 6 20:55 .recently-used.xbel -rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc drwxr-xr-x. 2 root root 4096 May 6 08:44 Templates drwxr-xr-x. 2 root root 4096 May 6 08:44 Videos -rw-------. 1 root root 3992 Jun 6 14:15 .viminfo -r--r--r--. 1 root root 60638465 May 6 08:46 VMwareTools-9.6.1-1378637.tar.gz drwxr-xr-x. 7 root root 4096 Oct 18 2013 vmware-tools-distrib
取消别名
[root@node1~]# unalias la [root@node1~]# la -bash:la: command not found
alias对命令设置别名之后仅仅在当前shell中生效,如果想保存这一设定的话,就必须将其保存在配置文件中
[root@node1~]# echo " alias la='ls -al'" >> /etc/bashrc
此操作会对全局生效,如果仅仅相对某个用户生效可以
[root@node1~]# echo " alias la='ls -al'" >> ~/.bashrc
4,whatis,有了whereis 和which,有没有what,why,when呢,答案是没有,但是有个whatis,而且这个whatis可能和你们想的不一样
[root@node1 ~]#whatis cd cd (1p) - change the working directory cd [builtins] (1) - bash built-in commands, see bash(1) cd-drive (1) - manual page for cd-drive cd-info (1) - manual page for cd-info cd-paranoia 9.8 (Paranoia release III via libcdio) [cd-paranoia](1) - an audio CD reading utility whichincludes extra data verification features cd-read (1) - manual page for cd-read version 0.81
原来这个是查看这个命令在man文档中1-9这九个章节中的哪个章节的
[root@node1 ~]# whatis passwd passwd (1) - update user's authentication tokens passwd (5) - password file passwd [sslpasswd] (1ssl) - compute password hashes
有的时候你可能会遇到这样的情况
[root@node1 ~]# whatis passwd passwd: nothing appropriate
你只需要makewhatis一下就可以了
[root@node1 ~]# makewhatis
最后再说一说命令的搜索路径,大家都知道当在shell中输入命令之后,shell就会在文件系统中寻找这个命令对应的文件,而对于这个寻找的路径大家应该也很熟悉了,就是$PATH这一环境变量
[root@node1 ~]# echo $PATH /usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
但是实际上shell在搜索$PATH之前会先去别的地方搜索,而$PATH其实是最后搜索的地方,shell真正的搜索路径应该是
1, alias
2, hashtable
3, function
4, builtins
5, $PATH
1. alias,如果用户定义了别名的话,那么别名会先被搜索到并且执行,不论是别名是否被保存至配置文件都是如此
[root@node1 ~]# alias la='ls -al' [root@node1 ~]# la total 59428 dr-xr-x---. 22 root root 4096 Jun 6 14:15 . dr-xr-xr-x. 25 root root 4096 Jun 7 08:34 .. drwxr-xr-x. 2 root root 4096 May 6 08:44 .abrt -rw-------. 1 root root 1592 May 7 04:40 anaconda-ks.cfg -rw-------. 1 root root 534 Jun 6 14:16 .bash_history -rw-r--r--. 1 root root 18 May 20 2009 .bash_logout -rw-r--r--. 1 root root 176 May 20 2009 .bash_profile -rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc drwxr-xr-x. 2 root root 4096 May 6 08:46 .cache drwx------. 4 root root 4096 May 6 08:44 .config -rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc drwx------. 3 root root 4096 May 6 08:44 .dbus drwxr-xr-x. 2 root root 4096 May 6 08:44 Desktop drwxr-xr-x. 2 root root 4096 May 6 08:44 Documents drwxr-xr-x. 2 root root 4096 May 6 08:44 Downloads -rw-------. 1 root root 16 May 6 08:44 .esd_auth drwx------. 4 root root 4096 May 6 20:53 .gconf drwx------. 2 root root 4096 May 6 20:55 .gconfd drwx------. 5 root root 4096 May 6 08:44 .gnome2 -rw-r--r--. 1 root root 107 May 6 20:53 .gtk-bookmarks drwx------. 2 root root 4096 May 6 08:44 .gvfs -rw-------. 1 root root 620 May 6 20:53 .ICEauthority -rw-r--r--. 1 root root 623 May 6 20:53 .imsettings.log -rw-r--r--. 1 root root 48228 May 7 04:40 install.log -rw-r--r--. 1 root root 9360 May 7 04:37 install.log.syslog drwxr-xr-x. 3 root root 4096 May 6 08:44 .local drwxr-xr-x. 2 root root 4096 May 6 08:44 Music drwxr-xr-x. 2 root root 4096 May 6 08:44 .nautilus drwxr-xr-x. 2 root root 4096 May 6 08:44 Pictures drwxr-xr-x. 2 root root 4096 May 6 08:44 Public drwx------. 2 root root 4096 May 6 08:44 .pulse -rw-------. 1 root root 256 May 6 08:44 .pulse-cookie -rw-------. 1 root root 218 May 6 20:55 .recently-used.xbel -rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc drwxr-xr-x. 2 root root 4096 May 6 08:44 Templates drwxr-xr-x. 2 root root 4096 May 6 08:44 Videos -rw-------. 1 root root 3992 Jun 6 14:15 .viminfo -r--r--r--. 1 root root60638465 May 6 08:46VMwareTools-9.6.1-1378637.tar.gz drwxr-xr-x. 7 root root 4096 Oct 18 2013 vmware-tools-distri
2. hashtable
[root@node1 ~]# hash hits command1 /bin/date1 /usr/bin/vim1 /bin/ls
hashtable其实是用户在执行命令之后shell将搜索的结果的缓存,这样可以提高搜索的效率和速度,但是hash是不会记录shell的内部命令,functions以及定义路径的别名的
那什么叫定义了路径的别名呢
[root@node1 ~]# hash -r [root@node1 ~]# hash hash: hash table empty
清空hash表
[root@node1 ~]# alias ls='ls' [root @node1 ~]# ls anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos VMwareTools-9.6.1-1378637.tar.gz vmware-tools-distrib [root@node1 ~]# hash hits command1 /bin/ls [root@node1 ~]# unalias ls [root@node1 ~]# hash hits command 1 /bin/ls
为ls指定了没有指定路径的别名后,hash结果为1
[root@node1 ~]# alias ls='/bin/ls' [root@node1 ~]# ls anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos VMwareTools-9.6.1-1378637.tar.gz vmware-tools-distrib [root@node1 ~]# hash hits command 1 /bin/ls
在取消了第一次别名之后再为ls指定一个指定了路径的别名之后,hash结果仍然为一并未发生变化,证明这一次指定路径的别名并没有被缓存只hashtable中
[root@node1 ~]# unalias ls [root@node1 ~]# ls anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos VMwareTools-9.6.1-1378637.tar.gz vmware-tools-distrib [root@node1 ~]# hash hits command2 /bin/ls [root@node1 ~]#
取消别名再执行ls之后,hashtable中变为2
3,function
function指的是一些用户自定义的函数,且此函数名和shell的命令名称相同,此时shell会优先执行用户自定义的函数
[root@node1 ~]# function cd { echo"just for a test";} [root@node1 ~]# cd just for a test
在用户自定了cd这函数之后再输入cd就是执行刚才用户定义的函数而不是shell命令cd了
要是想执行原本在shell中cd命令改怎么办呢只需要在cd前加上builtin就可以
[root@node1 ~]# builtin cd / [root@node1 /]#
type -a cd可以看到以下结果
[root@node1 /]# type -a cd cd is a function cd () {echo "just for a test" } cd is a shell builtin
cd不仅是一个shell内建的命令,而且是用户定义的函数
4,builtins,接下来是检索的是shell的内建的命令
5, 最后才是$PATH中定义的搜索路径
[root@node1 /]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
即以下几个路径
/usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /sbin:/bin /usr/sbin /usr/bin /root/bin
转载于:https://blog.51cto.com/viperstars/1423293