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() 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., Copyright (C) - 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., Copyright (C) - 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   实际搜寻硬盘查询文件名称。

最新文章

  1. MySql中in和exists效率
  2. 大数相乘nyoj28
  3. MFCC常用类介绍
  4. 宠物AI(个人觉得有问题)
  5. substr_replace()函数:将手机号中间4位隐藏为*号
  6. C# 中的枚举类型 enum (属于值类型)
  7. 网页调用外部APP
  8. Android android-common 常用功能和工具集合
  9. VMware vSphere 6 Enterprise Plus License
  10. hdu1281棋盘游戏
  11. Redis数据类型之字符串String
  12. TIKV副本一致性检查机制分析
  13. [NOIp2008] 双栈排序 (二分图染色 + 贪心)
  14. tornado中form表单验证详解
  15. 【Clojure 基本知识】 关于函数参数的各种高级用法
  16. date格式互转
  17. Webview窗口设置遮罩层
  18. k64 datasheet学习笔记3---Chip Configuration之Clock modules
  19. confirm提示弹出确定和取消按钮
  20. 使用solr报错

热门文章

  1. 压力测试衡量CPU的三个指标:CPU Utilization、Load Average和Context Switch Rate
  2. python按行读取apk中版本号、包名等信息
  3. json字符串转为json对象-jQuery.parseJSON()
  4. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
  5. $on、$emit和$broadcast的使用
  6. 连接redis失败,关闭防火墙即可
  7. Maven的配置地址
  8. Jmeter拓展插件(jmeter-plugins)
  9. HDFS源码分析数据块复制选取复制源节点
  10. IOS开发中的分享到邮件