文章目录

  • 一. which命令介绍
  • 二. 语法格式及常用选项
  • 三. 参考案例
    • 3.1 显示命令的路径
    • 3.2 显示命令的别名
    • 3.3 普通用户和root用户查找路径不同
  • 四. /bin,/sbin,/usr/sbin,/usr/bin 目录区别
  • 总结

前言🚀🚀🚀
想要学好Linux,命令是基本功,企业中常用的命令大约200多个,不管是写shell脚本还是管理操作系统,最常用的命令必须要牢牢掌握,像我们以前学乘法口诀一样,烂熟于心,唯有如此,才能打牢基础。
💓 知识最重要的是记忆
💓 入门须知: 想要人生从容,必须全力以赴,努力才是你最终的入场券🚀🚀🚀
💕 最后: 努力成长自己,愿我们都能在看不到的地方闪闪发光 ,一起加油进步🍺🍺🍺

一. which命令介绍

which 命令用于查找并显示给定命令的绝对路径,环境变量 PATH 中保存了查找命令时需要遍历的目录。

which 指令会在环境变量 $PATH 设置的目录里查找符合条件的文件。也就是说,使用 which 命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

二. 语法格式及常用选项

我们可以用which which来查看which命令在哪个位置:

[root@mufengxiaoyue ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
	/usr/bin/alias
	/usr/bin/which

which查看帮助,我们可以使用 --help查看:

[root@mufengxiaoyue ~]# 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>.

把常用的参数整理成图表如下:

参数描述
-a查找全部内容,而非第一个文件
-n<文件名长度>  指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
-p<文件名长度>  与-n参数相同,但此处的<文件名长度>包括了文件的路径。
-w指定输出时栏位的宽度。
-V显示版本信息。
–version, -[vV]显示版本信息并退出
–help
–skip-dot跳过 PATH 中以点开头的目录
–skip-tilde跳过 PATH 中以波形符号开头的目录
–show-dot不要在输出中将点扩展到当前目录
–show-tilde为 HOME 目录(非根目录)输出波形
–tty-only如果不在 tty 上,停止右边的处理选项
–all, -a打印 PATH 中的所有匹配项,而不仅仅是第一个
–read-alias, -i从 stdin 中读取别名列表
–skip-alias忽略选项 --read-alias;不读 stdin
–read-functions从 stdin 读取 shell 函数
–skip-functions忽略选项 --read-functions;不读 stdin

也可以使用man which 查看相关的内容

三. 参考案例

3.1 显示命令的路径

此时,无需加任何参数,比如我们要显示 find的路径

[root@mufengxiaoyue ~]# which find
/usr/bin/find

可以看到find的路径在/usr/bin/find、我们使用ls查看是否正确

[root@mufengxiaoyue ~]# ls /usr/bin/find
/usr/bin/find

如果我们把find命令移走会怎么样?

发现移走后就找不到了:

root@mufengxiaoyue ~]# which find
/usr/bin/which: no find in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)

如果改名会怎么样?

当然了,改名字也是不行的,不信你看:

[root@mufengxiaoyue ~]# mv /usr/bin/find /usr/bin/find.bak
[root@mufengxiaoyue ~]# which find
/usr/bin/which: no find in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)

说明:which 是根据使用者所配置的 PATH 变量内的目录去搜寻可运行文件!所以,不同的 PATH 配置内容所找到的命令可能不一样的
w

3.2 显示命令的别名

如果变量有别名,会直接显示变量的别名

[root@mufengxiaoyue ~]# which rm
alias rm='rm -i'
	/usr/bin/rm

rm的别名是rm -i

3.3 普通用户和root用户查找路径不同

## root用户的时候
[root@mufengxiaoyue ~]# which cd
/usr/bin/cd
## 普通用户的时候
[root@mufengxiaoyue ~]# useradd mufeng
[root@mufengxiaoyue ~]# su - mufeng
[mufeng@mufengxiaoyue ~]$  which cd
/bin/cd
[mufeng@mufengxiaoyue ~]$ 

四. /bin,/sbin,/usr/sbin,/usr/bin 目录区别

我们经常看到这几个目录,他们都是用来存放命令的。

/sbin 和/bin

1. 从命令功能区分

  • /sbin 下的命令属于基本的系统命令,如shutdown,reboot,用于启动系统,修复系统
  • /bin下存放一些普通的基本命令,如ls,chmod等,这些命令在Linux系统里的配置文件脚本里经常用到

2. 从用户权限角度区分

  • /sbin目录下的命令通常只有管理员才可以运行
  • /bin下的命令管理员和一般的用户都可以使用。

转一个网友的解读,个人觉得解释的很到位

/bin是系统的一些指令,主要放置一些系统的必备执行命令
比如:

cat、cp、chmod df、dmesg、gzip、kill、ls、mkdir、more、mount、rm、su、tar等

/sbin一般是指超级用户指令。 主要放置一些系统管理的必备程式
比如

dump、fdisk、halt、ifconfig、ifup、 ifdown、init、insmod ,lsmod、reboot、shutdown 等。

/usr/bin 是你在后期安装的一些软件的运行脚本。主要放置一些应用软体工具的必备执行档
比如:

c++、g++、gcc、make wget 等

/usr/sbin 放置一些用户安装的系统管理的必备程式
例如

dhcpd、httpd、imap、、inetd、named、netconfig、samba、sendmail、squid、swap、tcpdump 等

这些东西记起来比较麻烦,最好的办法就是用which查看,看看属于哪个目录。

总结

以上就是关于which命令的全部内容,你学会了吗?

💕💕💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!✨ ✨ ✨
🍻🍻🍻如果你喜欢的话,就不要吝惜你的一键三连了~


更多推荐

linux基本功系列-which命令实战