LOADING...

加载中请稍等...

loading

Linux每日一篇 - 35 - whereis

Linux文件位置查找神器!掌握whereis命令,快速定位二进制文件、源码和手册页,让文件查找更高效!

whereis命令是什么?

whereis命令用于在特定标准目录中查找二进制文件、源代码文件和手册页文件的位置。与which命令不同,whereis搜索更广泛的目录,包括标准的系统目录。

基本用法

# 查找命令的二进制文件、源码和手册页位置
whereis command_name

# 只查找二进制文件
whereis -b command_name

# 只查找手册页
whereis -m command_name

# 只查找源代码
whereis -s command_name

# 显示详细信息
whereis -u command_name  # 显示未找到的类型
whereis -f command_name  # 显示完整路径

# 限制搜索路径
whereis -B /custom/path -f command_name  # 只在指定路径下搜索二进制文件
whereis -M /usr/share/man -f command_name  # 只在指定路径下搜索手册页
whereis -S /usr/src -f command_name  # 只在指定路径下搜索源代码

实用技巧

# 查找常用系统命令的位置
whereis ls
whereis ps
whereis top
whereis df

# 查找开发工具
whereis gcc
whereis make
whereis gdb
whereis python

# 查找系统服务相关文件
whereis apache2
whereis nginx
whereis ssh
whereis cron

# 查找库文件
whereis libssl
whereis libcrypto

# 查找多个命令
whereis ls cp mv grep

# 搜索手册页位置
whereis -m bash
whereis -m gcc
whereis -m socket

# 检查是否安装了某个软件包的源代码
whereis -s nginx

# 使用whereis查找系统文件
whereis passwd
whereis hosts
whereis fstab

# 列出所有类型文件的路径
whereis -bm command_name

# 检查系统命令的完整性
whereis -u command_name  # 显示缺失的文件类型

常用场景

# 检查开发环境
whereis gcc g++ make cmake

# 查找网络工具位置
whereis curl wget ssh ping

# 系统管理工具位置
whereis useradd passwd groupmod

# 查找Python相关文件
whereis python python-config

# 定位配置文件模板
whereis fstab hosts services

# 检查系统服务组件
whereis systemd init upstart

# 查找压缩工具
whereis tar gzip bzip2 zip

# 验证系统命令安装完整
whereis -bm ls  # 检查二进制文件和手册页都存在

# 查找库函数手册
whereis -m printf malloc

# 查找系统头文件位置
whereis -s socket  # 查找socket相关源码

与其他查找命令的对比

# whereis vs which vs locate vs find:

# whereis - 搜索标准目录下的二进制文件、源码和手册页
whereis ls

# which - 只在PATH中搜索可执行文件
which ls

# locate - 在文件数据库中快速搜索文件
locate /etc/passwd

# find - 在指定路径下搜索文件
find /usr -name ls -type f

# type - 显示命令的类型(别名、函数、内置命令等)
type ls

高级用法

# 自定义搜索路径
whereis -B /opt/bin -M /opt/man -f myapp

# 批量查找多个程序
programs=(git curl wget docker-compose node npm)
for program in "${programs[@]}"; do
    echo "=== $program ==="
    whereis "$program"
done

# 检查命令的完整性
check_command() {
    local cmd=$1
    echo "Checking $cmd:"
    whereis -b "$cmd"  # 二进制文件
    whereis -m "$cmd"  # 手册页
    whereis -s "$cmd"  # 源码
}

# 搜索系统中的开发工具
whereis gcc g++ clang make cmake autotools

# 查找与安全相关的命令
whereis iptables firewall-cmd selinux

# 检查系统性能监控工具
whereis top htop iotop vmstat iostat

# 查找网络配置工具
whereis ifconfig netstat ss ip route

限制和注意事项

# 1. whereis只在预定义的标准目录中搜索
# 标准二进制目录: /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin
# 标准手册目录: /usr/share/man, /usr/local/man
# 标准源码目录: /usr/src, /usr/local/src

# 2. 不会搜索用户自定义目录
# 对于用户安装的软件,可能需要使用find命令

# 3. 搜索结果受系统配置影响
# 不同的Linux发行版可能有不同的标准目录

# 4. whereis不搜索当前目录
# 这与which和find不同

# 5. 搜索速度快但范围有限
# 如果需要更广泛的搜索,使用find或locate命令

实际应用示例

# 脚本中验证系统工具
validate_system_tools() {
    tools=(gcc make git curl wget)
    for tool in "${tools[@]}"; do
        result=$(whereis "$tool")
        if [[ $result == *"$tool:"* ]]; then
            echo "✓ $tool 已安装: $result"
        else
            echo "✗ $tool 未找到"
        fi
    done
}

# 查找系统管理命令
system_admin_commands=(useradd userdel groupadd groupmod passwd chage)
for cmd in "${system_admin_commands[@]}"; do
    whereis "$cmd"
done

# 检查网络配置文件
network_files=(hosts networks resolv.conf)
for file in "${network_files[@]}"; do
    whereis "$file"
done

# 查找服务管理工具
whereis systemctl service initd

关于我

全平台同名”汪多多是只猫”,专注分享实用技术教程,让你的IT学习之路更轻松!

关注我,每天一个Linux命令,轻松入门Linux系统!

头像
汪多多是只猫
失业的运维工程师
热爱开源与分享
微信公众号