Linux whereis 命令实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何在 Linux 中使用 whereis 命令来定位系统中各种程序的可执行文件、源代码和手册页。whereis 命令是一个非常有用的工具,可以快速找到命令或程序的位置,因为它会搜索预定义的标准二进制目录列表。你还将学习如何自定义 whereis 命令的搜索行为,以满足你的特定需求。

本实验涵盖以下步骤:

  1. 理解 whereis 命令的用途
  2. 使用 whereis 定位可执行文件、源代码和手册页
  3. 自定义 whereis 命令的搜索行为

whereis 命令是 Linux 用户的一个强大工具,本实验将通过实际示例和深入讲解,帮助你在日常工作中有效地使用它。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux/FileandDirectoryManagementGroup -.-> linux/locate("File Locating") linux/FileandDirectoryManagementGroup -.-> linux/which("Command Locating") linux/FileandDirectoryManagementGroup -.-> linux/whereis("File/Command Finding") subgraph Lab Skills linux/locate -.-> lab-423006{{"Linux whereis 命令实用示例"}} linux/which -.-> lab-423006{{"Linux whereis 命令实用示例"}} linux/whereis -.-> lab-423006{{"Linux whereis 命令实用示例"}} end

理解 whereis 命令的用途

在这一步中,你将学习 Linux 中 whereis 命令的用途和用法。whereis 命令是一个非常有用的工具,用于定位给定命令或程序的可执行文件、源代码和手册页。

whereis 命令会在预定义的标准二进制目录列表中搜索程序,例如 /bin/usr/bin/usr/sbin。它可以帮助你快速找到系统中命令或程序的位置。

让我们从运行 whereis 命令来定位 ls 命令开始:

whereis ls

示例输出:

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

输出显示,ls 命令的可执行文件位于 /usr/bin/ls 目录中,而对应的手册页位于 /usr/share/man/man1/ls.1.gz 文件中。

whereis 命令还可以用于定位程序的源代码。例如,要查找 gcc 编译器的源代码,你可以运行:

whereis gcc

示例输出:

gcc: /usr/bin/gcc /usr/lib/gcc /usr/include/gcc /usr/share/man/man1/gcc.1.gz

此输出表明,gcc 的可执行文件位于 /usr/bin/gcc 目录中,而源代码和包含文件分别位于 /usr/lib/gcc/usr/include/gcc 目录中。

whereis 命令是一个强大的工具,可以快速定位 Linux 系统中与特定程序或命令相关的文件。

使用 whereis 定位可执行文件、源代码和手册页

在这一步中,你将学习如何使用 whereis 命令来定位 Linux 系统中各种程序的可执行文件、源代码和手册页。

让我们从查找 gcc 编译器的位置开始:

whereis gcc

示例输出:

gcc: /usr/bin/gcc /usr/lib/gcc /usr/include/gcc /usr/share/man/man1/gcc.1.gz

此输出显示,gcc 的可执行文件位于 /usr/bin/gcc 目录中,源代码文件位于 /usr/lib/gcc 目录中,而手册页位于 /usr/share/man/man1/gcc.1.gz 文件中。

接下来,让我们查找 python3 可执行文件的位置:

whereis python3

示例输出:

python3: /usr/bin/python3 /usr/lib/python3.10 /usr/share/man/man1/python3.1.gz

输出表明,python3 可执行文件位于 /usr/bin/python3 目录中,Python 3.10 库文件位于 /usr/lib/python3.10 目录中,而手册页位于 /usr/share/man/man1/python3.1.gz 文件中。

你可以使用 whereis 命令来定位系统中与任何程序或命令相关的文件。当你需要查找特定可执行文件、源代码或手册页的位置时,这将特别有用。

自定义 whereis 命令的搜索行为

在这一步中,你将学习如何自定义 whereis 命令的搜索行为,以更好地满足你的需求。

默认情况下,whereis 命令会在预定义的目录列表中搜索程序,例如 /bin/usr/bin/usr/sbin。然而,你可以通过使用 -b-m-s 选项来修改搜索行为。

让我们来探索这些选项:

  1. -b (binaries):此选项告诉 whereis 仅搜索二进制可执行文件。
whereis -b gcc

示例输出:

gcc: /usr/bin/gcc
  1. -m (manual):此选项告诉 whereis 仅搜索手册页。
whereis -m gcc

示例输出:

gcc: /usr/share/man/man1/gcc.1.gz
  1. -s (source):此选项告诉 whereis 仅搜索源代码文件。
whereis -s gcc

示例输出:

gcc: /usr/lib/gcc

你还可以组合这些选项以进一步自定义搜索。例如,要搜索 python3 命令的二进制文件和手册页,可以使用:

whereis -b -m python3

示例输出:

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

通过使用这些选项,你可以根据特定需求定制 whereis 命令,并快速定位你需要的文件。

总结

在本实验中,你首先学习了 Linux 中 whereis 命令的用途和用法。whereis 命令是一个非常有用的工具,用于定位给定命令或程序的可执行文件、源代码和手册页。你通过实践使用 whereis 命令查找了 lsgcc 命令的位置,并发现它可以帮助你快速识别系统中与程序相关的文件。

接下来,你深入学习了如何使用 whereis 命令来定位各种程序的可执行文件、源代码和手册页。你探索了 whereis 命令如何在预定义的标准二进制目录列表中搜索文件,并展示了它如何为像 gcc 这样的程序提供必要的信息以找到相关文件。

Linux 命令速查表