问答题556/1053whereis 命令有哪些常见用法

难度:
2021-11-02 创建

参考答案:

whereis 命令是一个用于查找程序二进制文件、源代码文件和帮助文件(手册页等)的 Linux 命令。它可以帮助用户快速定位文件的位置,特别是对于命令、程序和手册页的查找。

基本语法:

1whereis [选项] [文件名]

常见用法:

1. 查找命令或程序的路径

  • 默认情况下,whereis 查找的是程序的二进制文件、源代码文件和手册页。
    1whereis command_name
    例如:
    1whereis ls
    输出:
    1ls: /bin/ls /usr/share/man/man1/ls.1.gz
    该命令会显示 ls 命令的二进制文件路径和手册页路径。

2. 只查找二进制文件

  • 使用 -b 选项来仅查找二进制文件:
    1whereis -b command_name
    例如:
    1whereis -b ls
    该命令只会返回 ls 命令的二进制文件路径(例如 /bin/ls)。

3. 只查找源代码文件

  • 使用 -s 选项来仅查找源代码文件:
    1whereis -s command_name
    例如:
    1whereis -s ls
    该命令会返回 ls 命令的源代码文件路径(如果有的话)。

4. 只查找手册页

  • 使用 -m 选项来仅查找手册页文件:
    1whereis -m command_name
    例如:
    1whereis -m ls
    该命令会返回 ls 命令的手册页路径(例如 /usr/share/man/man1/ls.1.gz)。

5. 查找多个文件

  • whereis 也可以查找多个文件,多个文件名可以用空格分隔:
    1whereis command_name1 command_name2 ...
    例如:
    1whereis ls find
    该命令会同时查找 lsfind 命令的路径。

6. 查找指定目录中的文件

  • whereis 默认在系统的标准目录中查找文件,但你可以通过环境变量调整查找路径。使用 -f 选项来指定查找的目录:
    1whereis -f /path/to/directory command_name

7. 查找配置文件

  • 如果你想查找程序的配置文件,可以指定配置文件名:
    1whereis --config filename

示例:

  1. 查找 ls 命令的路径:

    1whereis ls

    输出:

    1ls: /bin/ls /usr/share/man/man1/ls.1.gz

    这里返回的是 ls 命令的二进制文件路径和手册页路径。

  2. 查找 gcc 命令的二进制文件:

    1whereis -b gcc

    输出:

    1gcc: /usr/bin/gcc

    只返回 gcc 的二进制文件路径。

  3. 查找 bash 命令的源代码:

    1whereis -s bash

    输出:

    1bash: /usr/src/bash-4.4

    返回 bash 源代码的位置(如果有的话)。

  4. 查找 vim 的手册页:

    1whereis -m vim

    输出:

    1vim: /usr/share/man/man1/vim.1.gz

    仅返回 vim 命令的手册页路径。

最近更新时间:2024-12-24