每天2个Linux命令 which whereis


2017-07-21 每天2个Linux which命令

which命令用于查找并显示给定命令的绝对路径。

环境变量PATH中保存了查找命令时需要遍历的目录。

which指令会在环境变量$PATH设置的目录里查找符合条件的文件。

也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。


(1)用法:

用法:  which  [选项参数] [命令名]

(2)功能:

功能:查找环境变量中的文件

(3)选项参数:

  1) -n             指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。

    2) -p                                   与-n参数相同,但此处的包括了文件的路径。

    3) -w                                   指定输出时栏位的宽度。

    4) -V                                   显示版本信息**

(4)实例:

  1)[root@localhost sunjimeng]# which pwd    查看命令所在目录

[root@localhost sunjimeng]# which pwd
/usr/bin/pwd
[root@localhost sunjimeng]# which head
/usr/bin/head
[root@localhost sunjimeng]# which cat
/usr/bin/cat
[root@localhost sunjimeng]# which adduser
/usr/sbin/adduser

2)[root@localhost sunjimeng]# which which 用which命令找which命令

[root@localhost sunjimeng]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /usr/bin/alias
    /usr/bin/which

3)[root@localhost sunjimeng]# which –help

复制代码
[root@localhost sunjimeng]# which --help
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
  --help,          Print this help and exit successfully.
  --skip-dot       Skip directories in PATH that start with a dot.
  --skip-tilde     Skip directories in PATH that start with a tilde.
  --show-dot       Don't expand a dot to current directory in output.
  --show-tilde     Output a tilde for HOME directory for non-root.
  --tty-only       Stop processing options on the right if not on tty.
  --all, -a        Print all matches in PATH, not just the first
  --read-alias, -i Read list of aliases from stdin.
  --skip-alias     Ignore option --read-alias; don't read stdin.
  --read-functions Read shell functions from stdin.
  --skip-functions Ignore option --read-functions; don't read stdin.

Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.

If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.

Report bugs to <which-bugs@gnu.org>.

4)[root@localhost sunjimeng]# which –version

[root@localhost sunjimeng]# which --version
GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood.
GNU which comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.
[root@localhost sunjimeng]# which -V
GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood.
GNU which comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.

5)其他:

说明:

1.which 是根据使用者所配置的 PATH 变量内的目录去搜寻可运行档的!所以,不同的 PATH 配置内容所找到的命令当然不一样的!

2.竟然会有两个 which ,其中一个是 alias 这就是所谓的『命令别名』,意思是输入 which 会等於后面接的那串命令!

3.bash内建命令:

  1).什么是build in命令:

shell内建命令是指bash(或其它版本)工具集中的命令。一般都会有一个与之同名的系统命令,
比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他们行为大体相仿。
当在bash中键入一个命令时系统会先看他是否是一个内建命令,如果不是才会查看是否是系统命令或第三方工具。
所以在bash中键入echo命令实际上执行bash工具集中的bash命令也就是内建命令,而不是/bin/echo这个系统命令。

  2).内建命令与系统命令 内建命令要比系统论命令有比较高的执行效率。外部命令执行时往往需要fork出(产生出)一个子进程,而内建命令一般不用。

  3).查看一个命令是系统命令还是内建命令:type

复制代码
[root@localhost sunjimeng]# type -a pwd
pwd 是 shell 内嵌
pwd 是 /usr/bin/pwd
pwd 是 /bin/pwd
[root@localhost sunjimeng]# type -a echo
echo 是 shell 内嵌
echo 是 /usr/bin/echo
echo 是 /bin/echo
复制代码
  可以看出,有些命令,echo和pwd同时是内建命令和系统命令。

    4.补充:

 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: 
   which  查看可执行文件的位置。
   whereis 查看文件的位置。 
   locate   配合数据库查看文件位置。
   find   实际搜寻硬盘查询文件名称。

2017-07-21 每天2个Linux whereis命令

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)

和源代码文件(参数-s)。如果省略参数,则返回所有信息。


(1)用法:

用法:    whereis  [-bmsu]   [BMS 目录名 -f ]    文件名

(2)功能:

功能:    用来定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。

(3)选项参数:

  1) -b:                            只查找二进制文件

  2) -B<目录>:                 只在设置的目录下查找二进制文件

  3) -f:                            不显示文件名前的路径名称

  4) -m:                          只查找说明文件

  5) -M<目录>:                只在设置的目录下查找说明文件

  6) -s:            只查找原始代码文件

    7) -S<目录>:         只在设置的目录下查找原始代码文件

    8) -u:              查找不包含指定类型的文件

(4)实例:

  1)[sunjimeng@localhost ~]$ whereis cd    查找与cd命令有关的所有文件,包括二进制,man说明文件,源代码文件。

[sunjimeng@localhost ~]$ whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
[sunjimeng@localhost ~]$ whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz

2)[sunjimeng@localhost ~]$ whereis -b|-s|-m cd

[sunjimeng@localhost ~]$ whereis -b cd
cd: /usr/bin/cd
[sunjimeng@localhost ~]$ whereis -s cd
cd:[sunjimeng@localhost ~]$ whereis -m cd
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

3)[sunjimeng@localhost ~]$ whereis -f cd

[sunjimeng@localhost ~]$ whereis -f cd                         //好像没什么区别啊
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
[sunjimeng@localhost ~]$ whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

4)[sunjimeng@localhost ~]$ whereis whereis

复制代码
[sunjimeng@localhost ~]$ whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
[sunjimeng@localhost ~]$ whereis -b whereis
whereis: /usr/bin/whereis
[sunjimeng@localhost ~]$ whereis -m whereis
whereis: /usr/share/man/man1/whereis.1.gz
[sunjimeng@localhost ~]$ whereis -s whereis

(5)其他:

说明:

和find相比,whereis查找的速度非常快,这是因为linux系统会将 
系统内的所有文件都记录在一个数据库文件中,当使用whereis和下面即将介绍的locate时,
会从数据库中查找数据,而不是像find命令那样,通 过遍历硬盘来查找,效率自然会很高。 
但是该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用whereis和locate 
查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录