linux whereis命令

命令简介

whereis 命令用于定位命令的二进制文件、源代码文件和手册页的位置。它会在标准的系统目录中搜索指定命令的相关文件,提供比 which 命令更全面的命令信息。

特点: whereis 命令专门搜索二进制文件、手册页和源代码,不依赖于 PATH 环境变量,而是在硬编码的标准目录中搜索。

语法

whereis [选项] 文件名...

常用形式:

# 查找命令的所有相关文件
whereis ls

# 查找多个命令
whereis ls cp mv

# 只查找特定类型的文件
whereis -b ls

常用选项

选项 说明
-b 只搜索二进制文件
-m 只搜索手册页文件
-s 只搜索源代码文件
-u 搜索不寻常的条目(即没有指定类型的文件)
-B 目录列表 指定二进制文件的搜索目录
-M 目录列表 指定手册页的搜索目录
-S 目录列表 指定源代码的搜索目录
-f 终止目录列表,后面跟文件名
-l 显示whereis搜索的目录列表
-h, --help 显示帮助信息
-V, --version 显示版本信息

搜索目录

whereis 命令在以下标准目录中搜索文件:

默认搜索路径
# 查看whereis搜索的目录
whereis -l
# 输出:
# bin: /usr/bin /usr/sbin /usr/lib /usr/lib64 /usr/local/bin ...
# man: /usr/share/man /usr/local/man ...
# src: /usr/src /usr/local/src ...
常见搜索目录
二进制文件目录:
/usr/bin, /usr/sbin, /bin, /sbin
/usr/local/bin, /usr/local/sbin
/usr/games, /usr/local/games

手册页目录:
/usr/share/man, /usr/local/man
/usr/X11R6/man

源代码目录:
/usr/src, /usr/local/src
/usr/src/linux, /usr/src/redhat

基本用法

1. 查找命令的所有相关文件
# 查找ls命令的所有文件
whereis ls
# 输出:ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

# 查找gcc编译器
whereis gcc
# 输出:gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

# 查找python
whereis python
# 输出:python: /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.8 ...
2. 查找特定类型的文件
# 只查找二进制文件
whereis -b ls
# 输出:ls: /usr/bin/ls

# 只查找手册页
whereis -m ls
# 输出:ls: /usr/share/man/man1/ls.1.gz

# 只查找源代码
whereis -s bash
# 输出:bash: (如果没有源代码,可能没有输出)
3. 查找多个命令
# 一次性查找多个命令
whereis ls cp mv rm
# 输出:
# ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
# cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
# mv: /usr/bin/mv /usr/share/man/man1/mv.1.gz
# rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz

# 查找开发工具
whereis gcc g++ make gdb
# 输出:
# gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz
# g++: /usr/bin/g++ /usr/share/man/man1/g++.1.gz
# make: /usr/bin/make /usr/share/man/man1/make.1.gz
# gdb: /usr/bin/gdb /usr/share/man/man1/gdb.1.gz
4. 自定义搜索目录
# 在自定义目录中搜索二进制文件
whereis -B /usr/local/bin -f python

# 搜索多个自定义目录
whereis -B /opt/bin:/home/user/bin -f myapp

# 组合不同类型的搜索
whereis -B /usr/bin -M /usr/share/man -f bash

实际应用场景

场景1:系统管理工具定位
# 查找系统管理命令
whereis systemctl journalctl hostnamectl timedatectl
# 输出:
# systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
# journalctl: /usr/bin/journalctl /usr/share/man/man1/journalctl.1.gz
# hostnamectl: /usr/bin/hostnamectl /usr/share/man/man1/hostnamectl.1.gz
# timedatectl: /usr/bin/timedatectl /usr/share/man/man1/timedatectl.1.gz

# 查找网络工具
whereis ip ss ping traceroute netstat
# 输出:
# ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz
# ss: /usr/sbin/ss /usr/share/man/man8/ss.8.gz
# ping: /usr/bin/ping /usr/share/man/man8/ping.8.gz
# traceroute: /usr/bin/traceroute /usr/share/man/man8/traceroute.8.gz
# netstat: /usr/bin/netstat /usr/share/man/man8/netstat.8.gz
场景2:开发环境检查
#!/bin/bash
# 检查开发工具是否完整安装

echo "=== 开发工具检查 ==="

TOOLS=("gcc" "g++" "python" "python3" "node" "npm" "java" "javac")

for tool in "${TOOLS[@]}"; do
    result=$(whereis "$tool")
    if [ -n "$result" ] && [ "$result" != "$tool:" ]; then
        echo "✓ $tool: 已安装"
        # 显示二进制文件位置
        bin_path=$(whereis -b "$tool" | cut -d' ' -f2)
        if [ -n "$bin_path" ]; then
            echo "  二进制: $bin_path"
        fi
    else
        echo "✗ $tool: 未安装"
    fi
done
场景3:软件包信息查询
# 查询已安装软件的文件位置
whereis nginx
# 输出:nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx ...

whereis apache2
# 输出:apache2: /usr/sbin/apache2 /etc/apache2 /usr/share/apache2 ...

whereis mysql
# 输出:mysql: /usr/bin/mysql /usr/share/mysql ...

# 查找数据库工具
whereis mysqldump mysqladmin mysqlcheck
# 输出:
# mysqldump: /usr/bin/mysqldump /usr/share/man/man1/mysqldump.1.gz
# mysqladmin: /usr/bin/mysqladmin /usr/share/man/man1/mysqladmin.1.gz
# mysqlcheck: /usr/bin/mysqlcheck /usr/share/man/man1/mysqlcheck.1.gz
场景4:手册页快速访问
#!/bin/bash
# 快速打开命令的手册页

cmd=$1

# 查找命令的手册页
man_page=$(whereis -m "$cmd" | awk '{print $2}')

if [ -n "$man_page" ]; then
    echo "打开手册页: $man_page"
    man "$cmd"
else
    echo "未找到 $cmd 的手册页"

    # 检查命令是否存在
    if whereis -b "$cmd" | grep -q "/"; then
        echo "命令存在但没有手册页"
    else
        echo "命令不存在"
    fi
fi

高级用法

1. 批量命令检查
#!/bin/bash
# 批量检查系统命令的完整性

echo "系统命令完整性检查"
echo "========================"

# 定义必要的系统命令
ESSENTIAL_COMMANDS=("ls" "cp" "mv" "rm" "cat" "grep" "find" "ps" "top" "df")

for cmd in "${ESSENTIAL_COMMANDS[@]}"; do
    echo -n "检查 $cmd: "

    # 检查二进制文件
    bin=$(whereis -b "$cmd" | awk '{print $2}')
    # 检查手册页
    man=$(whereis -m "$cmd" | awk '{print $2}')

    if [ -n "$bin" ]; then
        echo -n "✓二进制 "
    else
        echo -n "✗二进制 "
    fi

    if [ -n "$man" ]; then
        echo -n "✓手册页"
    else
        echo -n "✗手册页"
    fi

    echo
done
2. 查找不完整的安装
# 查找只有二进制文件没有手册页的命令
whereis -u -m ls

# 查找所有不寻常的条目(缺少某种类型文件)
whereis -u ls cp mv

# 检查特定命令的完整性
check_command() {
    local cmd=$1
    echo "检查 $cmd:"

    local bin=$(whereis -b "$cmd" | awk '{print $2}')
    local man=$(whereis -m "$cmd" | awk '{print $2}')
    local src=$(whereis -s "$cmd" | awk '{print $2}')

    [ -n "$bin" ] && echo "  二进制: $bin" || echo "  二进制: 未找到"
    [ -n "$man" ] && echo "  手册页: $man" || echo "  手册页: 未找到"
    [ -n "$src" ] && echo "  源代码: $src" || echo "  源代码: 未找到"
}

check_command "bash"
check_command "docker"
3. 与find命令结合
# 使用whereis找到大致位置,再用find精确查找
bin_dir=$(whereis -b python | awk '{print $2}' | xargs dirname)
find "$bin_dir" -name "python*" -type f

# 查找所有手册页的位置并统计
man_dir=$(whereis -m ls | awk '{print $2}' | xargs dirname)
find "$man_dir" -name "*.gz" | wc -l

# 在whereis找到的目录中进一步搜索
whereis -l | grep "^bin:" | cut -d: -f2 | tr ' ' '\n' | while read dir; do
    if [ -d "$dir" ]; then
        echo "目录 $dir 中有 $(ls "$dir" | wc -l) 个文件"
    fi
done
4. 自定义搜索路径
#!/bin/bash
# 扩展whereis的搜索路径

# 添加自定义目录到搜索路径
CUSTOM_BIN_DIRS="/opt/bin:/home/$USER/.local/bin"
CUSTOM_MAN_DIRS="/opt/man:/home/$USER/.local/share/man"

echo "扩展搜索路径..."
whereis -B "$CUSTOM_BIN_DIRS" -M "$CUSTOM_MAN_DIRS" -f "$@"

# 或者在环境中设置
export WHEREIS_BIN_PATH="$CUSTOM_BIN_DIRS"
export WHEREIS_MAN_PATH="$CUSTOM_MAN_DIRS"

与其他命令的比较

命令 功能 搜索范围 输出内容
whereis 定位二进制、手册页、源代码 标准系统目录 二进制、手册页、源代码路径
which 查找可执行文件 PATH环境变量 第一个匹配的可执行文件路径
locate 快速文件查找 整个文件系统(基于数据库) 所有匹配的文件路径
find 文件查找 指定目录及其子目录 匹配的文件路径
type 显示命令类型 shell环境 命令类型(别名、函数、内置、外部)

实用技巧

  • 使用 whereis -b 快速找到命令的二进制文件位置
  • 使用 whereis -m 查找命令的手册页位置
  • 组合使用 whereisman 快速查看命令文档
  • 使用 whereis -u 检查命令安装是否完整
  • 在脚本中使用 whereis 检查命令依赖
  • 使用 whereis -l 查看系统默认的搜索路径
  • 对于自定义安装的软件,使用 -B, -M, -S 选项指定搜索目录

注意事项

  • whereis 只搜索硬编码的标准目录,不搜索所有目录
  • 如果命令安装在非标准目录,whereis 可能找不到
  • whereis 不会找到 shell 内置命令
  • 某些命令可能没有手册页或源代码
  • whereis 的搜索结果可能因Linux发行版而异
  • 对于自定义编译安装的软件,可能需要手动更新搜索路径
  • whereis 通常比 which 提供更全面的信息