1.命令簡介
whereis 命令用於查找命令的二進制程序、源代碼文件和 man 手冊等相關文件的路徑,如果僅顯示命令的絕對路徑,可使用 which 命令,其他普通文件的查找需使用 locate 或 find 命令。
whereis 默認的搜索路徑是從硬編碼路徑中查找文件,硬編碼路徑是用 glob patterns 定義的,以及環境變量 PATH 和 MANPATH 定義的路徑。要知道使用了哪些路徑,最簡單的方法是使用 -l 選項來查看。
2.命令格式
whereis [OPTIONS] [-BMS DIRECTORY... -f] NAME...
whereis 會將提供的文件名前面的路徑與後面的擴展名去掉,並找出與之相匹配的特殊文件。
3.選項說明
-b
只搜索二進制文件
-m
只搜索 man 手冊
-s
只搜索源代碼
-u
只展示有相關特殊文件(二進制程序、源代碼文件、man 手冊)的命令名稱
-B LIST
指定二進制文件查找目錄,使用空格分隔
-M LIST
指定 man 手冊查找目錄,使用空格分隔
-S LIST
指定源代碼文件查找目錄,使用空格分隔
-f
在使用 -S 、–M、-B 選項時,必須使用這個選項,用於表示目錄的結束,命令名的開始
-l
輸出正在使用的有效查找路徑
4.常用示例
(1)查看命令 which 的二進制文件與幫助手冊的路徑。
whereis which
which: /usr/bin/which /usr/share/man/man1/which.1.gz
(2)只搜索二進制文件 which 的路徑。
whereis -b which
which: /usr/bin/which
(3)只搜索命令 which 幫助手冊的路徑。
whereis -m which
(4)搜索當前目錄下所有文件時,只展示具有相關特殊文件的文件名。
# 當前目錄下的文件
ll
-rwxr-xr-x 1 root root 21915 May 25 10:03 a.out
-rw-r--r-- 1 root root 351 May 25 10:03 main.cpp
-rw-r--r-- 1 root root 965 Nov 7 18:20 moretest.txt
-rw--w--w- 1 root root 316 Nov 5 15:33 passwd.md5
# 使用選項 -u 的結果
whereis -u *
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
# 不使用選項 -u 的結果
whereis *
a:main:moretest:passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
(5)指定查找命令 passwd 的目錄爲 /usr/bin。
whereis -b -B /usr/bin -f passwd
passwd: /usr/bin/passwd
(6)查看 whereis 默認的搜索路徑。
whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
...
參考文獻
[1] whereis manual
[2] 實例講解Linux whereis 命令