在linux上安装了某个应用时,是否会偶尔找不到应用的某些文件位置???
可以通过find命令查找,但是也可以通过whereis,which来快速定位。
whereis
whereis可以定位二进制文件,源文件,说明文件,查找时不需要文件的前置路径和后置扩展,whereis会尝试在标准的linux位置,$PATH 和 $MANPATH中定位我们想要的程序。并且whereis可以同时分别查找多个文件。例如, 在man whereis
有如下例子,
tangs@ubuntu:~$ whereis -bm ls tr -m gcc
ls: /bin/ls /usr/share/man/man1/ls.1.gz
tr: /usr/bin/tr /usr/share/man/man1/tr.1.gz
gcc: /usr/share/man/man1/gcc.1.gz
这就是查找ls,tr的二进制文件路径和说明文件路径,同时只查找gcc的说明文件路径。
whereis提供以下可输入参数:
参数 | 说明 |
---|---|
-b | 查找二进制位文件 |
-m | 查找说明文件 |
-s | 查找源文件 |
-u | 只显示有不寻常的条目的命令;如果一个命令对于一个明确的请求类型不是只有一个条目(即0和多个),则称这个命令是不寻常的。 |
-B directory | 指定目录查找二进制文件 |
-M directory | 指定目录查找说明文件 |
-S directory | 指定目录查找源文件 |
-f | 将需要查找的路径和需要查找的命令隔开,必须和 -B,-M,-S 一起使用 |
-l | 列出whereis查找命令时的有效的查找路径 |
-h | 查看帮助信息 |
-V | 查看版本信息 |
以上参数中值得注意的是-f
参数,它的原文解释为:
Terminates the directory list and signals the start of filenames. It must be used when any of the -B, -M, or -S options is used.
但是一些blog和教程网站的解释为不显示文件名前的路径名称
,如下图,我是觉得他们都解释错了。这应该是解释为在使用 -B,-M,-S时指定的路径是多个,查找的文件也是多个,那就需要一个-f
参数将 查找的路径和查找的文件隔开,形如:
# where -BMS 目录 -f 文件名
tangs@ubuntu:/bin$ whereis -S /bin/ /usr/local -f ls ps
ls: /bin/ls /usr/share/man/man1/ls.1.gz /bin/ls
ps: /bin/ps /usr/share/man/man1/ps.1.gz /bin/ps
which
which是一个很简单的命令,它就是在当前环境变量中查找可被执行文件或者可被执行文件链接的路径,最终就是在$PATH中查找路径,whereis命令已经可以做到这一点了。
实例:
tangs@ubuntu:/bin$ which ls ps
/bin/ls
/bin/ps