本文档风哥主要介绍Linux系统中which和whereis命令的使用方法,包括命令路径查找、命令类型判断、命令管理等,结合RHEL LINUX 10系统环境,提供详细的命令示例和输出结果,适合Linux运维人员在学习和生产环境中使用。更多视频教程www.fgedu.net.cn
参考Red Hat Enterprise Linux 10官方文档中的System administration章节
Part01-基础概念与理论知识
1.1 which/whereis命令的基本概念
which命令用于查找并显示指定命令的绝对路径,它会在PATH环境变量指定的目录中搜索可执行文件。whereis命令用于查找二进制文件、源代码文件和帮助文档文件的位置。这两个命令是Linux系统中常用的命令查找工具,可以帮助用户快速定位命令的位置。
- which:查找可执行文件的路径
- whereis:查找二进制、源码、手册页
- which:只搜索PATH中的目录
- whereis:搜索标准系统目录
- which:速度快,结果精确
- whereis:信息全面,包含文档
1.2 which/whereis命令的语法格式
which和whereis命令的基本语法格式如下:
which [选项] 命令名…
# whereis命令基本语法
whereis [选项] 文件名…
# 语法说明:
# 命令名:要查找的命令名称
# 文件名:要查找的文件名称
# 可以同时指定多个命令或文件名
1.3 which/whereis命令的常用参数
which和whereis命令的常用参数如下:
# -a, –all
# 显示所有匹配的命令路径
which -a ls
# –version
# 显示版本信息
which –version
# –help
# 显示帮助信息
which –help
# whereis命令常用参数
# -b
# 只查找二进制文件
whereis -b ls
# -m
# 只查找手册页文件
whereis -m ls
# -s
# 只查找源代码文件
whereis -s ls
# -u
# 查找不包含指定类型文件的文件
whereis -u ls
# -B <目录>
# 只在指定目录中查找二进制文件
whereis -B /usr/bin -b ls
# -M <目录>
# 只在指定目录中查找手册页文件
whereis -M /usr/share/man -m ls
# -S <目录>
# 只在指定目录中查找源代码文件
whereis -S /usr/src -s ls
# -f
# 终止目录列表,开始文件名列表
whereis -B /usr/bin -f ls
# –version
# 显示版本信息
whereis –version
# –help
# 显示帮助信息
whereis –help
Part02-生产环境规划与建议
2.1 命令查找场景规划
在生产环境中,which和whereis命令主要用于以下场景:
# 1. 命令定位场景
# – 查找命令的安装位置
# – 确认命令是否存在
# – 查找命令的多个版本
# – 查找命令的文档位置
# 2. 脚本开发场景
# – 检查命令是否可用
# – 获取命令的绝对路径
# – 判断命令类型
# – 验证命令版本
# 3. 系统管理场景
# – 查找系统命令位置
# – 查找配置文件位置
# – 查找文档位置
# – 查找源码位置
# 4. 故障排查场景
# – 确认命令是否安装
# – 检查PATH配置
# – 查找命令冲突
# – 验证命令权限
2.2 命令查找安全建议
在生产环境中使用which和whereis命令时,需要注意以下安全建议: 学习交流加群风哥QQ113257174
- 注意PATH环境变量的安全性
- 避免使用相对路径执行命令
- 检查命令的权限和所有者
- 注意命令是否被篡改
- 使用绝对路径执行关键命令
- 定期检查系统命令的完整性
2.3 命令查找最佳实践
which和whereis命令在生产环境中的最佳实践:
# 1. 在脚本中使用绝对路径
# CMD=$(which command)
# $CMD –version
# 2. 检查命令是否存在
# if ! which command >/dev/null 2>&1; then
echo “Command not found”
exit 1
fi
# 3. 查找命令的所有版本
# which -a python
# 4. 查找命令的文档
# whereis -m command
# 5. 验证命令的完整性
# ls -l $(which command)
# 6. 检查命令的类型
# type command
Part03-生产环境项目实施方案
3.1 which命令实战操作
which命令在RHEL LINUX 10系统中的实际操作示例: 更多学习教程公众号风哥教程itpux_com
# 1. 查找命令的路径
# which ls
/usr/bin/ls
# 2. 查找多个命令的路径
# which ls cat grep
/usr/bin/ls
/usr/bin/cat
/usr/bin/grep
# 3. 查找命令的所有路径
# which -a python
/usr/bin/python
/usr/local/bin/python
# 4. 查找系统命令
# which systemctl
/usr/bin/systemctl
# 5. 查找服务管理命令
# which httpd
/usr/sbin/httpd
# 6. 查找用户命令
# which useradd
/usr/sbin/useradd
# 7. 查找网络命令
# which ip
/usr/sbin/ip
# 8. 查找编辑器命令
# which vim
/usr/bin/vim
# 9. 查找Shell内置命令(无结果)
# which cd
/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
# 10. 查找别名
# alias ll=’ls -l’
# which ll
alias ll=’ls -l’
/usr/bin/ls
# 11. 查找脚本命令
# cat > /usr/local/bin/myscript.sh << 'EOF'
#!/bin/bash
echo "Hello World"
EOF
# chmod +x /usr/local/bin/myscript.sh
# which myscript.sh
/usr/local/bin/myscript.sh
# 12. 查找Python命令
# which python3
/usr/bin/python3
# 13. 查找Docker命令
# which docker
/usr/bin/docker
# 14. 查找Kubernetes命令
# which kubectl
/usr/bin/kubectl
# 15. 查找Ansible命令
# which ansible
/usr/bin/ansible
3.2 whereis命令实战操作
whereis命令在RHEL LINUX 10系统中的实际操作示例:
# 1. 查找命令的所有信息
# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
# 2. 只查找二进制文件
# whereis -b ls
ls: /usr/bin/ls
# 3. 只查找手册页文件
# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
# 4. 只查找源代码文件
# whereis -s ls
ls:
(大多数系统命令没有源代码)
# 5. 查找多个命令
# whereis ls cat grep
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz
# 6. 查找系统服务命令
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
# 7. 查找网络命令
# whereis ip
ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz
# 8. 查找编辑器命令
# whereis vim
vim: /usr/bin/vim /usr/share/vim /usr/share/man/man1/vim.1.gz
# 9. 查找Python命令
# whereis python
python: /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.9 /usr/share/man/man1/python.1.gz
# 10. 查找Docker命令
# whereis docker
docker: /usr/bin/docker /usr/libexec/docker /usr/share/man/man1/docker.1.gz
# 11. 查找Kubernetes命令
# whereis kubectl
kubectl: /usr/bin/kubectl /usr/share/man/man1/kubectl.1.gz
# 12. 查找Apache命令
# whereis httpd
httpd: /usr/sbin/httpd /usr/share/man/man8/httpd.8.gz
# 13. 查找MySQL命令
# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/man/man1/mysql.1.gz
# 14. 查找Nginx命令
# whereis nginx
nginx: /usr/sbin/nginx /usr/share/man/man8/nginx.8.gz
# 15. 查找Git命令
# whereis git
git: /usr/bin/git /usr/share/git /usr/share/man/man1/git.1.gz
3.3 高级用法实战
which和whereis命令的高级用法和技巧: 学习交流加群风哥微信: itpux-com
# 1. 在脚本中检查命令是否存在
# cat > /tmp/check_command.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if ! which $COMMAND >/dev/null 2>&1; then
echo “Error: $COMMAND not found”
exit 1
fi
echo “$COMMAND found at: $(which $COMMAND)”
EOF
# chmod +x /tmp/check_command.sh
# /tmp/check_command.sh ls
ls found at: /usr/bin/ls
# 2. 获取命令的绝对路径
# LS_PATH=$(which ls)
# echo $LS_PATH
/usr/bin/ls
# 3. 检查命令的多个版本
# which -a python python3
/usr/bin/python
/usr/bin/python3
# 4. 查找命令并显示详细信息
# ls -l $(which ls)
-rwxr-xr-x. 1 root root 140344 Apr 2 10:00 /usr/bin/ls
# 5. 查找命令并检查权限
# stat $(which ls)
File: /usr/bin/ls
Size: 140344 Blocks: 280 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1234567 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:bin_t:s0
Access: 2026-04-02 10:00:00.000000000 +0800
Modify: 2026-04-02 10:00:00.000000000 +0800
Change: 2026-04-02 10:00:00.000000000 +0800
Birth: 2026-04-02 10:00:00.000000000 +0800
# 6. 查找命令的文档
# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
# 查看手册页
# man ls
# 7. 查找命令的源代码
# whereis -s bash
bash:
(大多数系统命令没有源代码)
# 8. 查找命令的配置文件
# find /etc -name “*ls*”
/etc/profile.d/ls.bash
# 9. 检查命令是否被篡改
# rpm -Vf $(which ls)
(无输出表示文件未被修改)
# 10. 查找命令的依赖库
# ldd $(which ls)
linux-vdso.so.1 (0x00007ffc1234)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f1234)
libc.so.6 => /lib64/libc.so.6 (0x00007f5678)
libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007f9012)
/lib64/ld-linux-x86-64.so.2 (0x00007f3456)
# 11. 查找命令的包名
# rpm -qf $(which ls)
coreutils-8.32-23.el10.x86_64
# 12. 查找命令的所有者
# ls -l $(which ls) | awk ‘{print $3}’
root
# 13. 查找命令的SELinux上下文
# ls -lZ $(which ls)
-rwxr-xr-x. 1 root root system_u:object_r:bin_t:s0 140344 Apr 2 10:00 /usr/bin/ls
# 14. 查找命令的符号链接
# ls -l $(which python)
lrwxrwxrwx. 1 root root 7 Apr 2 10:00 /usr/bin/python -> python3
# 15. 查找命令的版本
# $(which ls) –version
ls (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Richard M. Stallman and David MacKenzie.
Part04-生产案例与实战讲解
4.1 命令管理案例
使用which和whereis命令进行命令管理:
# 1. 检查命令是否安装
# cat > /usr/local/bin/check_installed.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if [ -z "$COMMAND" ]; then
echo "Usage: $0
exit 1
fi
if which $COMMAND >/dev/null 2>&1; then
echo “$COMMAND is installed at: $(which $COMMAND)”
echo “Version: $($COMMAND –version 2>&1 | head -n 1)”
else
echo “$COMMAND is not installed”
fi
EOF
# chmod +x /usr/local/bin/check_installed.sh
# check_installed.sh docker
docker is installed at: /usr/bin/docker
Version: Docker version 20.10.12, build e91ed57
# 2. 查找命令的多个版本
# which -a python python3 python2
/usr/bin/python
/usr/bin/python3
/usr/bin/python2
# 3. 查找命令的文档位置
# whereis -m systemctl
systemctl: /usr/share/man/man1/systemctl.1.gz
# 查看文档
# man systemctl
# 4. 查找命令的配置文件
# find /etc -name “*systemctl*” 2>/dev/null
/etc/systemd/system.conf
# 5. 查找命令的依赖包
# rpm -qf $(which systemctl)
systemd-249-7.el10.x86_64
# 6. 查找命令的依赖库
# ldd $(which systemctl)
linux-vdso.so.1 (0x00007ffc1234)
libsystemd-shared-249.so => /lib64/libsystemd-shared-249.so (0x00007f1234)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5678)
libc.so.6 => /lib64/libc.so.6 (0x00007f9012)
/lib64/ld-linux-x86-64.so.2 (0x00007f3456)
# 7. 查找命令的SELinux上下文
# ls -lZ $(which systemctl)
-rwxr-xr-x. 1 root root system_u:object_r:systemd_exec_t:s0 123456 Apr 2 10:00 /usr/bin/systemctl
# 8. 查找命令的所有者
# ls -l $(which systemctl) | awk ‘{print $3″:”$4}’
root:root
# 9. 查找命令的权限
# ls -l $(which systemctl) | awk ‘{print $1}’
-rwxr-xr-x.
# 10. 查找命令的安装时间
# rpm -qi $(rpm -qf $(which systemctl)) | grep “Install Date”
Install Date: Thu 02 Apr 2026 10:00:00 AM CST Build Host: server01.fgedu.net
4.2 脚本应用案例
在Shell脚本中使用which和whereis命令: from LinuxDBA视频:www.itpux.com
# 1. 检查命令是否存在并执行
# cat > /tmp/run_command.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if ! which $COMMAND >/dev/null 2>&1; then
echo “Error: $COMMAND not found”
exit 1
fi
$COMMAND “${@:2}”
EOF
# chmod +x /tmp/run_command.sh
# /tmp/run_command.sh ls -l /tmp
# 2. 获取命令的绝对路径
# cat > /tmp/get_path.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if [ -z "$COMMAND" ]; then
echo "Usage: $0
exit 1
fi
PATH=$(which $COMMAND 2>/dev/null)
if [ -z “$PATH” ]; then
echo “Command not found: $COMMAND”
exit 1
fi
echo “Absolute path: $PATH”
EOF
# chmod +x /tmp/get_path.sh
# /tmp/get_path.sh ls
Absolute path: /usr/bin/ls
# 3. 检查多个命令是否安装
# cat > /tmp/check_commands.sh << 'EOF'
#!/bin/bash
COMMANDS="docker kubectl ansible"
for CMD in $COMMANDS; do
if which $CMD >/dev/null 2>&1; then
echo “[OK] $CMD: $(which $CMD)”
else
echo “[FAILED] $CMD: not installed”
fi
done
EOF
# chmod +x /tmp/check_commands.sh
# /tmp/check_commands.sh
[OK] docker: /usr/bin/docker
[OK] kubectl: /usr/bin/kubectl
[FAILED] ansible: not installed
# 4. 查找命令并显示详细信息
# cat > /tmp/command_info.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if [ -z "$COMMAND" ]; then
echo "Usage: $0
exit 1
fi
PATH=$(which $COMMAND 2>/dev/null)
if [ -z “$PATH” ]; then
echo “Command not found: $COMMAND”
exit 1
fi
echo “Command: $COMMAND”
echo “Path: $PATH”
echo “Package: $(rpm -qf $PATH)”
echo “Permissions: $(ls -l $PATH | awk ‘{print $1}’)”
echo “Owner: $(ls -l $PATH | awk ‘{print $3″:”$4}’)”
echo “Size: $(ls -lh $PATH | awk ‘{print $5}’)”
EOF
# chmod +x /tmp/command_info.sh
# /tmp/command_info.sh ls
Command: ls
Path: /usr/bin/ls
Package: coreutils-8.32-23.el10.x86_64
Permissions: -rwxr-xr-x.
Owner: root:root
Size: 140K
# 5. 查找命令的文档
# cat > /tmp/find_docs.sh << 'EOF'
#!/bin/bash
COMMAND=$1
if [ -z "$COMMAND" ]; then
echo "Usage: $0
exit 1
fi
DOCS=$(whereis -m $COMMAND | awk ‘{print $2}’)
if [ -z “$DOCS” ]; then
echo “No documentation found for: $COMMAND”
exit 1
fi
echo “Documentation for $COMMAND:”
echo “$DOCS”
EOF
# chmod +x /tmp/find_docs.sh
# /tmp/find_docs.sh ls
Documentation for ls:
/usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
4.3 故障排查案例
使用which和whereis命令进行故障排查:
# 1. 排查命令找不到的问题
# which command
/usr/bin/which: no command in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
# 解决方案:安装命令或检查PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 2. 排查命令版本问题
# which -a python
/usr/bin/python
/usr/local/bin/python
# 检查版本
# /usr/bin/python –version
Python 2.7.18
# /usr/local/bin/python –version
Python 3.9.7
# 3. 排查命令权限问题
# ls -l $(which command)
-rwx——. 1 root root 123456 Apr 2 10:00 /usr/bin/command
# 解决方案:修改权限
# chmod 755 /usr/bin/command
# 4. 排查命令被篡改问题
# rpm -Vf $(which command)
S.5….T. c /etc/command.conf
(文件被修改)
# 解决方案:重新安装包
# dnf reinstall package_name
# 5. 排查PATH配置问题
# echo $PATH
/usr/local/bin:/usr/bin
# 检查命令是否在其他目录
# find /usr -name “command” 2>/dev/null
/usr/sbin/command
# 解决方案:添加到PATH
# export PATH=$PATH:/usr/sbin
# 6. 排查命令冲突问题
# which -a command
/usr/local/bin/command
/usr/bin/command
# 检查两个命令的区别
# ls -l /usr/local/bin/command /usr/bin/command
-rwxr-xr-x. 1 root root 123456 Apr 2 10:00 /usr/local/bin/command
-rwxr-xr-x. 1 root root 123456 Apr 2 10:00 /usr/bin/command
# 7. 排查命令依赖问题
# ldd $(which command)
linux-vdso.so.1 (0x00007ffc1234)
libmissing.so.1 => not found
# 解决方案:安装缺失的库
# dnf install libmissing
# 8. 排查SELinux问题
# ls -lZ $(which command)
-rwxr-xr-x. 1 root root unconfined_u:object_r:default_t:s0 123456 Apr 2 10:00 /usr/bin/command
# 检查SELinux日志
# ausearch -m AVC -ts recent | grep command
# 解决方案:恢复正确的上下文
# restorecon -v /usr/bin/command
Part05-风哥经验总结与分享
5.1 命令查找技巧总结
根据多年的Linux运维经验,总结which和whereis命令的使用技巧:
# 1. 在脚本中使用绝对路径
# CMD=$(which command)
# $CMD –version
# 2. 检查命令是否存在
# if ! which command >/dev/null 2>&1; then
echo “Command not found”
exit 1
fi
# 3. 查找命令的所有版本
# which -a python
# 4. 查找命令的文档
# whereis -m command
# 5. 查找命令的详细信息
# ls -l $(which command)
# 6. 查找命令的包名
# rpm -qf $(which command)
# 7. 查找命令的依赖库
# ldd $(which command)
# 8. 查找命令的SELinux上下文
# ls -lZ $(which command)
5.2 命令查找检查清单
在生产环境中使用which和whereis命令时的检查清单:
- 确认命令是否存在
- 确认命令的路径
- 确认命令的权限
- 确认命令的所有者
- 确认命令的版本
- 确认命令的依赖
- 确认命令的文档
- 确认命令的完整性
- 确认PATH配置
- 确认SELinux上下文
5.3 相关工具推荐
与which和whereis命令相关的工具和替代方案:
# 1. type – 查找命令类型
# type ls
ls is aliased to `ls –color=auto’
# 优点:可以区分别名、函数、内置命令
# 2. command – 查找命令
# command -v ls
alias ls=’ls –color=auto’
# 优点:可以查找别名和函数
# 3. locate – 快速文件查找
# locate bin/ls
/usr/bin/ls
# 优点:速度快,使用数据库索引
# 4. find – 文件查找
# find /usr -name “ls” -type f
/usr/bin/ls
# 优点:可以查找任意文件,支持多种条件
# 5. apropos – 搜索手册页
# apropos ls
ls (1) – list directory contents
# 优点:可以搜索手册页描述
# 6. whatis – 查询命令描述
# whatis ls
ls (1) – list directory contents
# 优点:可以快速查看命令描述
# 7. rpm – 查询包信息
# rpm -qf /usr/bin/ls
coreutils-8.32-23.el10.x86_64
# 优点:可以查询文件所属的包
# 8. dnf – 查询包信息
# dnf provides /usr/bin/ls
coreutils-8.32-23.el10.x86_64 : A set of basic GNU tools commonly used in shell scripts
# 优点:可以查询文件所属的包和仓库
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
