which 命令在Linux中是一个快速查找可执行文件位置的工具

0、概念

  • which命令用于查找命令的可执行文件的路径
  • which 命令在 Linux 中用于查找可执行命令的完整路径。当你在 shell 中输入一个命令时,shell 会在环境变量 $PATH 定义的目录列表中查找这个命令。which 命令可以帮助你确定系统将执行哪个可执行文件
  • which命令在Linux中的用法是用于查找并显示指定命令的完整路径。它会在系统的PATH环境变量中搜索指定的命令,并返回第一个匹配的命令的完整路径。
  • 在Linux系统中,which命令用于查找指定命令的绝对路径。它会在环境变量PATH中搜索指定的命令,并返回找到的第一个匹配项的完整路径
  • 在Linux系统中,which 命令用于显示可执行文件的位置。当你在命令行中输入一个命令时,which 会告诉你这个命令的可执行文件位于哪个目录。这对于确定系统上安装了哪些程序以及它们的具体位置非常有用。
  • which 命令是一个快速查找可执行文件位置的工具,对于系统管理员和开发者来说非常有用。
  • 请注意,which 命令只能找到已安装且可执行的文件。如果找不到某个命令,可能是因为该命令未安装,或者不在系统的 PATH 变量中
  • which 命令仅搜索在 $PATH 环境变量中定义的目录。
  • 在某些情况下,which 命令可能不会找到通过shell函数或别名定义的命令。
which command_name

这里的 command_name 是你想要查找的命令名称。
命令名:要查找的可执行文件的名称。

1、which --help

[root@iZuf6332h890vozldoxcprZ sbin]# 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>.

2、which命令解释

which 命令在 Linux 中用于查找并显示指定命令的绝对路径。你通过运行 which --help 获得的帮助信息非常详细,下面我会对这些选项进行简要解释,以便你更好地理解它们的作用:

  • --version, -[vV]: 显示 which 命令的版本信息并正常退出。
  • --help: 显示帮助信息,即你当前看到的这个页面,然后正常退出。
  • --skip-dot: 在搜索 PATH 环境变量时,跳过以点(.)开头的目录。这通常用于忽略当前目录(. 表示当前目录)。
  • --skip-tilde: 在搜索 PATH 环境变量时,跳过以波浪线(~)开头的目录。这通常用于忽略用户的主目录(~ 通常代表用户的主目录)。
  • --show-dot: 在输出结果中,不将点(.)扩展为当前目录的完整路径。
  • --show-tilde: 对于非 root 用户,如果命令位于其主目录中,则在输出中使用波浪线(~)代替完整的主目录路径。
  • --tty-only: 如果不是在终端(tty)上运行,则停止处理右侧的选项。这通常用于脚本或批处理模式中,以确保只有在交互式终端会话中才处理某些选项。
  • --all, -a: 显示 PATH 环境变量中所有匹配的命令路径,而不仅仅是第一个找到的路径。
  • --read-alias, -i: 从标准输入读取别名列表。这允许 which 检查别名,并可能显示别名指向的命令的完整路径。
  • --skip-alias: 忽略 --read-alias 选项,不从标准输入读取别名。
  • --read-functions: 从标准输入读取 shell 函数定义。这允许 which 检查函数,并可能显示函数内部使用的命令的完整路径。
  • --skip-functions: 忽略 --read-functions 选项,不从标准输入读取函数。

推荐的使用方式是将 (alias; declare -f) 命令的输出写入标准输入,这样 which 可以显示别名和 shell 函数的完整路径。例如,你可以通过管道将 (alias; declare -f) 的输出传递给 which,但请注意,which 本身并不直接支持这样的用法;这里提到的推荐用法更多是一种概念上的指导,实际上你可能需要结合其他工具(如 grepawk 等)来实现类似的功能。

最后,如果你在使用 which 命令时遇到任何问题或发现任何 bug,可以向 <which-bugs@gnu.org> 报告。

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 22:38:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 22:38:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 22:38:02       45 阅读
  4. Python语言-面向对象

    2024-07-20 22:38:02       55 阅读

热门阅读

  1. NVM配置

    NVM配置

    2024-07-20 22:38:02      16 阅读
  2. 获取磁盘剩余容量-----c++

    2024-07-20 22:38:02       15 阅读
  3. springboot3.2 RedisCacheManager配置

    2024-07-20 22:38:02       16 阅读
  4. springSecurity学习之springSecurity简介

    2024-07-20 22:38:02       20 阅读
  5. 分布式锁-redisson锁重试和WatchDog机制

    2024-07-20 22:38:02       14 阅读
  6. Photoshop图层类型

    2024-07-20 22:38:02       14 阅读
  7. (一)js前端开发中设计模式前篇之对象

    2024-07-20 22:38:02       16 阅读
  8. 网络安全-网络安全及其防护措施6

    2024-07-20 22:38:02       15 阅读
  9. [C++ 入门基础 - 命名空间]

    2024-07-20 22:38:02       14 阅读