which命令用于查找并显示给定命令的绝对路径。
环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
(1)用法:
用法: which [选项参数] [命令名]
(2)功能:
功能:查找环境变量中的文件
(3)选项参数:
1) -n 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
2) -p 与-n参数相同,但此处的包括了文件的路径。
3) -w 指定输出时栏位的宽度。
4) -V 显示版本信息
(4)实例:
1)[root@localhost sunjimeng]# which pwd 查看命令所在目录
[root@localhost sunjimeng]# which pwd /usr/bin/pwd [root@localhost sunjimeng]# which head /usr/bin/head [root@localhost sunjimeng]# which cat /usr/bin/cat
[root@localhost sunjimeng]# which adduser
/usr/sbin/adduser
2)[root@localhost sunjimeng]# which which 用which命令找which命令
[root@localhost sunjimeng]# which which alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/alias /usr/bin/which
3)[root@localhost sunjimeng]# which --help
[root@localhost sunjimeng]# which --help Usage: /usr/bin/which [options] [--] COMMAND [...] Write the full path of COMMAND(s) to standard output. --version, -[vV] Print version and exit successfully. --help, Print this help and exit successfully. --skip-dot Skip directories in PATH that start with a dot. --skip-tilde Skip directories in PATH that start with a tilde. --show-dot Don't expand a dot to current directory in output. --show-tilde Output a tilde for HOME directory for non-root. --tty-only Stop processing options on the right if not on tty. --all, -a Print all matches in PATH, not just the first --read-alias, -i Read list of aliases from stdin. --skip-alias Ignore option --read-alias; don't read stdin. --read-functions Read shell functions from stdin. --skip-functions Ignore option --read-functions; don't read stdin. Recommended use is to write the output of (alias; declare -f) to standard input, so that which can show aliases and shell functions. See which(1) for examples. If the options --read-alias and/or --read-functions are specified then the output can be a full alias or function definition, optionally followed by the full path of each command used inside of those. Report bugs to <which-bugs@gnu.org>.
4)[root@localhost sunjimeng]# which --version
[root@localhost sunjimeng]# which --version GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood. GNU which comes with ABSOLUTELY NO WARRANTY; This program is free software; your freedom to use, change and distribute this program is protected by the GPL.
[root@localhost sunjimeng]# which -V GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood. GNU which comes with ABSOLUTELY NO WARRANTY; This program is free software; your freedom to use, change and distribute this program is protected by the GPL.
5)其他:
说明:
1.which 是根据使用者所配置的 PATH 变量内的目录去搜寻可运行档的!所以,不同的 PATH 配置内容所找到的命令当然不一样的!
2.竟然会有两个 which ,其中一个是 alias 这就是所谓的『命令别名』,意思是输入 which 会等於后面接的那串命令!
3.bash内建命令:
1).什么是build in命令:
shell内建命令是指bash(或其它版本)工具集中的命令。一般都会有一个与之同名的系统命令,比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他们行为大体相仿。当在bash中键入一个命令时系统会先看他是否是一个内建命令,如果不是才会查看是否是系统命令或第三方工具。所以在bash中键入echo命令实际上执行bash工具集中的bash命令也就是内建命令,而不是/bin/echo这个系统命令。
2).内建命令与系统命令 内建命令要比系统论命令有比较高的执行效率。外部命令执行时往往需要fork出(产生出)一个子进程,而内建命令一般不用。
3).查看一个命令是系统命令还是内建命令:type
[root@localhost sunjimeng]# type -a pwd pwd 是 shell 内嵌 pwd 是 /usr/bin/pwd pwd 是 /bin/pwd [root@localhost sunjimeng]# type -a echo echo 是 shell 内嵌 echo 是 /usr/bin/echo echo 是 /bin/echo
可以看出,有些命令,echo和pwd同时是内建命令和系统命令。
4.补充:
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:
which 查看可执行文件的位置。
whereis 查看文件的位置。
locate 配合数据库查看文件位置。
find 实际搜寻硬盘查询文件名称。