菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
348
0

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

原创
05/13 14:22
阅读数 95898

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 内所规范的目录,所以当然找不到!

发表评论

0/200
348 点赞
0 评论
收藏
为你推荐 换一批