每天一个linux命令(16):which

1、命令简介

which (which) 命令的作用是在PATH变量指定的路径中搜索某个系统命令的位置并且返回第一个搜索结果。也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪一个位置的命令

2、用法

/usr/bin/which [options] [--] COMMAND [...]

3、选项

  --version, -[vV]   显示版本信息
  --help,          显示帮助信息
  --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        查找全部匹配的,而非第一个
  --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.

4、示例

示例1:查看默认的jdk
[root@oracledb ~]# which java
/usr/bin/java
示例2:which查找的目录,PATH变量的值
[root@oracledb ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
实例3:查找有别名的命令时会列出具体的别名信息
[root@oracledb ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /usr/bin/which

用 which 去找出 which,结果会有两个 which ,其中一个是 alias 这就是所谓的『命令别名』

实例4:Bash内建命令无法查找到如cd命令
[root@oracledb ~]# 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)

cd 这个常用的命令找不到,这是因为 cd 是bash 内建的命令! 但是 which 默认是找 PATH 内所规范的目录,所以当然找不到!

posted on 2016-04-24 10:59  小强斋太  阅读(1638)  评论(0编辑  收藏  举报

导航