查找和命令有关的文件
一条命令相关的文件有二进制可执行文件、配置文件、帮助说明文间、源代码文件、共享库文件。我们经常需要修改一些命令服务的配置文件,
每个linux发行版放置配置文件的目录可能会有些差异,那么怎么找到一条命令的这些文件呢?
这就是通过which和whereis这两个命令了。
我们知道linux下的命令分为内置命令和外置命令,内置命令作为shell功能的一部分,可执行程序代码都存放在/bin/bash中。
而外置命令有自己对应的可执行程序文件,那么怎么知道一条命令的可执行程序文件存放在哪里呢?
我们可以通过which、whereis、type这些命令查找命令的可执行文件路径。
which命令查找外置命令的可执行文件。 which命令从$PATH环境变量指定的目录中查找命令的可执行文件,还记得$PATH环境变量环境变量吧? 我们可以通过echo命令显示$PATH环境变量的值:
which命令常用格式如下:
有时候$PATH目录中可能有不止一个文件和命令匹配,which默认只列出第一个找到的可执行文件,加上-a选项可以列出所有的可执行文件。 查找ifconfig命令的可执行文件路径:
whereis命令搜索和命令相关的文件。
whereis命令也是用来搜索命令的可执行文件的,不过whereis不止搜索命令的可执行文件,也会搜索和命令相关帮助说明文件、配置文件、命令共享库文件、命令的源码文件等。
which命令是到$PAHT环境变量指定的目录中搜索文件,而whereis命令也是到系统指定的某些目录中搜索文件,whereis搜索的目录主要包括三种:
bin:可执行文件所在的目录;
man:帮助说明文件man page所在的目录;
src:源码文件所在的目录。
可通过-l选项查看whereis命令搜索的具体目录。
whereis命令常用格式如下:
1.which命令
回到顶部which命令查找外置命令的可执行文件。 which命令从$PATH环境变量指定的目录中查找命令的可执行文件,还记得$PATH环境变量环境变量吧? 我们可以通过echo命令显示$PATH环境变量的值:
root@peter-VirtualBox:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games目录之间用:号分隔。关于环境变量和$PATH的更多内容请参考: 什么是linux命令 , linux shell变量与环境变量
which命令常用格式如下:
[root@initroot ~]# which [-a] command-a选项将$PATH目录中可以找到的所有命令文件均列出,而不是第一个被找到的命令文件。
有时候$PATH目录中可能有不止一个文件和命令匹配,which默认只列出第一个找到的可执行文件,加上-a选项可以列出所有的可执行文件。 查找ifconfig命令的可执行文件路径:
[root@initroot ~]# which ifconfig /sbin/ifconfigwhich本身也是一条命令,用which查找自己的可执行文件路径:
root@peter-VirtualBox:~# which which /usr/bin/which查找history命令的可执行文件路径:
root@peter-VirtualBox:~# which history root@peter-VirtualBox:~#没有任何反应,因为history是shell内置命令,可执行代码在shell的可执行文件/bin/bash中, 没有与之匹配的可执行文件存在,所以就没有任何输出了。
which命令用于查找命令文件的路径,which根据PATH环境变量所规定的路径,查找可执行文件的文件名,如果找到,默认显示第一个找到的文件名位置。
加上-a选项可以列出所有找到的同名可执行文件。
例如想知道cp这个命令的位置,可以输入如下命令查找:
which cp
命令执行结果如下:
peter@peter-VirtualBox:~$ which cp /bin/cp
SYNOPSIS
which [-a] filename ...
locate a command
which returns the pathnames of the files (or links) which would be executed in the current environ‐ ment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this by searching the PATH for executable files matching the names of the arguments. It does not canoni‐ calize path names.
OPTIONS
-a print all matching pathnames of each argument
EXIT STATUS
0 if all specified commands are found and executable
1 if one or more specified commands is nonexistent or not executable
2 if an invalid option is specified
2.whereis命令
回到顶部bin:可执行文件所在的目录;
man:帮助说明文件man page所在的目录;
src:源码文件所在的目录。
可通过-l选项查看whereis命令搜索的具体目录。
whereis命令常用格式如下:
[root@initroot ~]# whereis [-bmsu] 文件或目录名选项与参数:
-l :列出whereis命令的搜索目录;通过whereis -l查看whereis命令的搜索目录:
-b :只找命令的二进制可执行文件binary
-m :只找命令的帮助说明文件man page
-s :只找命令的源码文件
-u :搜寻不在上述三种目录中的其他特殊文件
root@peter-VirtualBox:~# whereis -l bin: /usr/bin bin: /usr/sbin bin: /lib/x86_64-linux-gnu bin: /usr/lib/x86_64-linux-gnu bin: /usr/lib bin: /bin bin: /sbin bin: /etc bin: /lib bin: /lib64 bin: /usr/games bin: /usr/local/bin bin: /usr/local/sbin bin: /usr/local/etc bin: /usr/local/lib bin: /usr/local/games bin: /usr/include bin: /usr/local bin: /usr/libexec bin: /usr/share bin: /opt/VBoxGuestAdditions-6.1.0/bin man: /usr/share/man/zh_TW man: /usr/share/man/da man: /usr/share/man/man5 man: /usr/share/man/man7 man: /usr/share/man/pt_BR man: /usr/share/man/pl man: /usr/share/man/man2 man: /usr/share/man/de man: /usr/share/man/es man: /usr/share/man/zh_CN man: /usr/share/man/tr man: /usr/share/man/man4 man: /usr/share/man/man1 man: /usr/share/man/hu man: /usr/share/man/gl man: /usr/share/man/sl man: /usr/share/man/it man: /usr/share/man/id man: /usr/share/man/ru man: /usr/share/man/pt man: /usr/share/man/cs man: /usr/share/man/fr.UTF-8 man: /usr/share/man/fi man: /usr/share/man/sr man: /usr/share/man/hr man: /usr/share/man/man8 man: /usr/share/man/ko man: /usr/share/man/nl man: /usr/share/man/sv man: /usr/share/man/ja man: /usr/share/man/man3 man: /usr/share/man/fr.ISO8859-1 man: /usr/share/man/man6 man: /usr/share/man/fr man: /usr/share/info src: /usr/src/linux-headers-5.0.0-32-generic src: /usr/src/linux-headers-4.15.0-74-generic src: /usr/src/linux-headers-4.15.0-54-generic src: /usr/src/linux-headers-4.15.0-74 src: /usr/src/linux-headers-4.15.0-54 src: /usr/src/linux-headers-5.0.0-32查找ifconfig命令相关的文件:
root@peter-VirtualBox:~# whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz查找passwd命令相关的文件:
root@peter-VirtualBox:~# whereis passwd passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz只列出和passwd命令相关的说明文件:
root@peter-VirtualBox:~# whereis -m passwd passwd: /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz
which命令根据PATH环境变量所规定的路径,查找可执行文件的文件名。
whereis命令也是在特定的目录下查找命令的可执行文件位置,whereis不仅查找可执行文件位置,同时也会查找源文件和命令说明手册的位置。
whereis ls
whereis pwd
peter@peter-VirtualBox:~$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz peter@peter-VirtualBox:~$ whereis pwd pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz可通过-l选项列出whereis的查询目录:
peter@peter-VirtualBox:~$ whereis -l bin: /usr/bin bin: /usr/sbin bin: /lib/x86_64-linux-gnu bin: /usr/lib/x86_64-linux-gnu bin: /usr/lib bin: /bin bin: /sbin bin: /etc bin: /lib bin: /lib64 bin: /usr/games bin: /usr/local/bin bin: /usr/local/sbin bin: /usr/local/etc bin: /usr/local/lib bin: /usr/local/games bin: /usr/include bin: /usr/local bin: /usr/libexec bin: /usr/share bin: /opt/VBoxGuestAdditions-5.2.30/bin man: /usr/share/man/ko man: /usr/share/man/man5 man: /usr/share/man/sr man: /usr/share/man/zh_TW man: /usr/share/man/da man: /usr/share/man/zh_CN man: /usr/share/man/pt_BR man: /usr/share/man/pt man: /usr/share/man/fr.UTF-8 man: /usr/share/man/it man: /usr/share/man/man8 man: /usr/share/man/man1 man: /usr/share/man/fr.ISO8859-1 man: /usr/share/man/de.UTF-8 man: /usr/share/man/sv man: /usr/share/man/ru man: /usr/share/man/gl man: /usr/share/man/id man: /usr/share/man/man2 man: /usr/share/man/de man: /usr/share/man/sl man: /usr/share/man/man4 man: /usr/share/man/man3 man: /usr/share/man/man7 man: /usr/share/man/tr man: /usr/share/man/nl man: /usr/share/man/cs man: /usr/share/man/fi man: /usr/share/man/hu man: /usr/share/man/man6 man: /usr/share/man/hr man: /usr/share/man/fr man: /usr/share/man/ja.UTF-8 man: /usr/share/man/pl man: /usr/share/man/es man: /usr/share/man/ja man: /usr/share/info src: /usr/src/linux-headers-4.15.0-20 src: /usr/src/linux-headers-4.15.0-65-generic src: /usr/src/linux-headers-4.15.0-65 src: /usr/src/linux-headers-4.15.0-54-generic src: /usr/src/linux-headers-4.15.0-20-generic src: /usr/src/linux-headers-4.15.0-54
Usage:
whereis [options] [-BMS <dir>... -f] <name>
Locate the binary, source, and manual-page files for a command.
Options:
-b search only for binaries
-B <dirs> define binaries lookup path
-m search only for manuals and infos
-M <dirs> define man and info lookup path
-s search only for sources
-S <dirs> define sources lookup path
-f terminate <dirs> argument list
-u search for unusual entries
-l output effective lookup paths
-h, --help display this help
-V, --version display version
For more details see whereis(1).
initroot编辑整理,转载请注明www.initroot.com
100次点赞
100次阅读