Linux下两个实用命令:which和whereis 的区别

Linux下的用户一般都了解的一个命令应给非man莫属吧,但怎样才能更好的发挥的它的最大效用,另一个很实用的命令whereis不得不提:

whereis:用来查询文件的二进制代码、源文件、man手册的位置等、

看一下在Linux下它的联机文档吧:

Code:
  1. wzb55@ubuntu:~/c$ man whereis  
  2.   
  3. WHEREIS(1)                                                          WHEREIS(1)  
  4.   
  5.   
  6.   
  7. NAME  // 命令的名字-功能简介
  8.        whereis  -  locate the binary, source, and manual page files for a com-  
  9.        mand //为一个命令(包括C语言中系统函数)定位其二进制文件,源文件 、man手册文件的位置。
  10.   
  11. SYNOPSIS  //用法大纲
  12.        whereis [ -bmsu ] [ -BMS directory...  -f ] filename ...  
  13.   
  14. DESCRIPTION  
  15.        whereis locates source/binary and manuals sections for specified files.  
  16.        The  supplied  names  are first stripped of leading pathname components  
  17.        and any (single) trailing extension of the form .ext, for example,  .c.  
  18.        Prefixes  of  s.   resulting  from  use of source code control are also  
  19.        dealt with.  whereis then attempts to locate the desired program  in  a  
  20.        list of standard Linux places.  
  21.   
  22. OPTIONS  
  23.        -b     Search only for binaries. //只搜索二进制文件的位置 
  24.   
  25.        -m     Search only for manual sections.  //只搜索man手册文件的位置
  26.   
  27.        -s     Search only for sources.     //只搜索源文件的位置
  28.   
  29.        -u     Search  for unusual entries.  A file is said to be unusual if it  
  30.               does  not  have  one  entry  of  each  requested   type.    Thus  
  31.               `whereis  -m  -u  *'  asks for those files in the current direc-  
  32.               tory which have no documentation.  //对模式取反
  33.   
  34.        -B     Change or otherwise limit the places where whereis searches  for  
  35.               binaries.  //改变或者限制 whereis 搜索目标命令二进制文件的目录(位置)
  36.   
  37.        -M     Change  or otherwise limit the places where whereis searches for  
  38.               manual sections.  //改变或者限制whereis 搜索目标命令man手册文件的目录(位置)
  39.   
  40.        -S     Change or otherwise limit the places where whereis searches  for  
  41.               sources.  //改变或者限制whereis 搜索目标命令源文件的目录(位置)
  42.   
  43.        -f     Terminate  the last directory list and signals the start of file  
  44.               names, and must be used when any of the -B, -M,  or  -S  options  
  45.               are used.  
  46.   
  47. EXAMPLE  
  48.        Find  all  files  in /usr/bin which are not documented in /usr/man/man1  
  49.        with source in /usr/src:  
  50.   
  51.               example% cd /usr/bin  
  52.               example% whereis -u -M /usr/man/man1 -S /usr/src -f *  
  53.   
  54. FILES  
  55.        /{bin,sbin,etc}  
  56.   
  57.        /usr/{lib,bin,old,new,local,games,include,etc,src,man,sbin,  
  58.                            X386,TeX,g++-include}  
  59.   
  60.        /usr/local/{X386,TeX,X11,include,lib,man,etc,bin,games,emacs}  
  61.   
  62. SEE ALSO  
  63.        chdir(2V)  
  64.   
  65. BUGS  
  66.        Since whereis uses chdir(2V) to run faster, pathnames  given  with  the  
  67.        -M, -S, or -B must be full; that is, they must begin with a `/'.  
  68.   
  69.        whereis has a hard-coded path, so may not always find what you're look-  
  70.        ing for.  
  71.   
  72. AVAILABILITY  
  73.        The whereis command is part of the util-linux-ng package and is  avail-  
  74.        able from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.  
  75.   
  76.   
  77.   
  78.                                   8 May 1994                        WHEREIS(1)  

1.在编写Shell脚本时,往往会用到命令的二进制文件的绝对路径:这时执行如下命令可以轻松搞定!

               whereis -b 命令名

例如:要找ls、pwd的命令的绝对路径

Code:
  1. wzb55@ubuntu:~/c$ whereis -b ls pwd  
  2. ls: /bin/ls  
  3. pwd: /bin/pwd /usr/include/pwd.h 

2.在C编程时,用到系统函数时,希望更精确地查看该函数的帮助,尤其是在命令和系统函数重名时更有用。

如果系统函数和命令不重名时,man 函数名  --可以查到该函数的具体用法。

如果系统函数和命令重名时, man 函数名 --查到的往往是命令的帮助手册,那么怎么找到该函数的手册呢??

whereis 可以帮我们轻松搞定:

例如: 查找gettimeofday()函数的参数说明和用法时,显然没有命令与该函数重名,那么直接: man gettimeofday

就可以获得 gettimeofday函数的man手册。

然而对于printf函数,如果直接 man printf 得到却是 printf Shell命令对应的man手册;那么怎么才可以获得

C语言中的printf函数的man手册呢? 用whereis -m printf  你会发现printf的man手册文件不至一个。

Code:
  1. wzb55@ubuntu:~/c$ whereis -m printf  
  2. printf: /usr/share/man/man1/printf.1.gz /usr/share/man/man3/printf.3.gz  
  3. //获得了manual文件那个是C语言中printf函数的呢?一般地较大的是。下面逐个验证

  4. wzb55@ubuntu:~/c$ man 1 printf  //user command 发现是Shell命令的printf
  5. wzb55@ubuntu:~/c$ man 3 printf  //Linux programer's Manual
  6. wzb55@ubuntu:~/c$

whereis 是不是对C编程很有用呀。

 

二、下面说一下 which命令吧!

Code:
  1. wzb55@ubuntu:~/c$ man which  
  2. WHICH(1)                                                              WHICH(1)  
  3.   
  4.   
  5.   
  6. NAME  //命令名--功能简介
  7.        which - locate a command  //定位一个命令  
  8.   
  9. SYNOPSIS  //用法大纲
  10.        which [-a] filename ...  
  11.   
  12. DESCRIPTION  //功能的详细描述
  13.        which returns the pathnames of the files which would be executed in the  
  14.        current environment, had its arguments been  given  as  commands  in  a  
  15.        strictly  POSIX-conformant  shell.   It does this by searching the PATH  
  16.        for executable files matching the names of the arguments. 
  17.            //which 返回符合POSIX标准的命令的可执行路径 
  18.   
  19. OPTIONS  //命令的选项说明
  20.        -a     print all matching pathnames of each argument  
  21.                //为每一个参数命令打印其所有的匹配路径
  22.   
  23. EXIT STATUS  //返回状态 在Linux下可以用 echo $?  查看一个执行完的程序的返回状态
  24.        0      if all specified commands are found and executable  
  25.                //所有指定命令都找到了可执行的路径
  26.   
  27.        1      if one or more specified commands is  nonexistent  or  not  exe-  
  28.               cutable  
  29.               //有一个或多于一个参数命令,是不存在或不可执行的,即找不到其路径
  30.   
  31.        2      if an invalid option is specified   // 指定了一个非法选项
  32.   
  33.   
  34.   
  35. Debian                            12 Jul 2004                         WHICH(1)  

可见which 与whereis -b 功能有相似之处:同样在编写Shell脚本时可以快速查找命令的绝对路径;

看下面which应用的举例:

Code:
  1. wzb55@ubuntu:~/c$  
  2. wzb55@ubuntu:~/c$ which time  
  3. /usr/bin/time  
  4. wzb55@ubuntu:~/c$ which -a time  
  5. /usr/bin/time  
  6. /usr/bin/X11/time  
  7. wzb55@ubuntu:~/c$ echo $?  
  8. 0  
  9. wzb55@ubuntu:~/c$ which ls  
  10. /bin/ls  
  11. wzb55@ubuntu:~/c$ which -a ls  
  12. /bin/ls  
  13. wzb55@ubuntu:~/c$ echo $?  
  14. 0  
  15. wzb55@ubuntu:~/c$ which printff  
  16. wzb55@ubuntu:~/c$ echo $?  
  17. 1  
  18. wzb55@ubuntu:~/c$ which -ap ls  
  19. Illegal option -p  
  20. Usage: /usr/bin/which [-a] args  
  21. wzb55@ubuntu:~/c$ echo $?  
  22. 2  
  23. wzb55@ubuntu:~/c$  

 

 

至此,学习了which ,whereis ,man 的一部分小功能,在Linux下进行C编程时,对于那些复杂的函数的参数,用法的记忆,就不怕啦,每次想看时,就man一下,瞅一眼,慢慢的就熟喽!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值