Linux下的用户一般都了解的一个命令应给非man莫属吧,但怎样才能更好的发挥的它的最大效用,另一个很实用的命令whereis不得不提:
whereis:用来查询文件的二进制代码、源文件、man手册的位置等、
看一下在Linux下它的联机文档吧:
- wzb55@ubuntu:~/c$ man whereis
- WHEREIS(1) WHEREIS(1)
- NAME // 命令的名字-功能简介
- whereis - locate the binary, source, and manual page files for a com-
- mand //为一个命令(包括C语言中系统函数)定位其二进制文件,源文件 、man手册文件的位置。
- SYNOPSIS //用法大纲
- whereis [ -bmsu ] [ -BMS directory... -f ] filename ...
- DESCRIPTION
- whereis locates source/binary and manuals sections for specified files.
- The supplied names are first stripped of leading pathname components
- and any (single) trailing extension of the form .ext, for example, .c.
- Prefixes of s. resulting from use of source code control are also
- dealt with. whereis then attempts to locate the desired program in a
- list of standard Linux places.
- OPTIONS
- -b Search only for binaries. //只搜索二进制文件的位置
- -m Search only for manual sections. //只搜索man手册文件的位置
- -s Search only for sources. //只搜索源文件的位置
- -u Search for unusual entries. A file is said to be unusual if it
- does not have one entry of each requested type. Thus
- `whereis -m -u *' asks for those files in the current direc-
- tory which have no documentation. //对模式取反
- -B Change or otherwise limit the places where whereis searches for
- binaries. //改变或者限制 whereis 搜索目标命令二进制文件的目录(位置)
- -M Change or otherwise limit the places where whereis searches for
- manual sections. //改变或者限制whereis 搜索目标命令man手册文件的目录(位置)
- -S Change or otherwise limit the places where whereis searches for
- sources. //改变或者限制whereis 搜索目标命令源文件的目录(位置)
- -f Terminate the last directory list and signals the start of file
- names, and must be used when any of the -B, -M, or -S options
- are used.
- EXAMPLE
- Find all files in /usr/bin which are not documented in /usr/man/man1
- with source in /usr/src:
- example% cd /usr/bin
- example% whereis -u -M /usr/man/man1 -S /usr/src -f *
- FILES
- /{bin,sbin,etc}
- /usr/{lib,bin,old,new,local,games,include,etc,src,man,sbin,
- X386,TeX,g++-include}
- /usr/local/{X386,TeX,X11,include,lib,man,etc,bin,games,emacs}
- SEE ALSO
- chdir(2V)
- BUGS
- Since whereis uses chdir(2V) to run faster, pathnames given with the
- -M, -S, or -B must be full; that is, they must begin with a `/'.
- whereis has a hard-coded path, so may not always find what you're look-
- ing for.
- AVAILABILITY
- The whereis command is part of the util-linux-ng package and is avail-
- able from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
- 8 May 1994 WHEREIS(1)
1.在编写Shell脚本时,往往会用到命令的二进制文件的绝对路径:这时执行如下命令可以轻松搞定!
whereis -b 命令名
例如:要找ls、pwd的命令的绝对路径
- wzb55@ubuntu:~/c$ whereis -b ls pwd
- ls: /bin/ls
- 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手册文件不至一个。
- wzb55@ubuntu:~/c$ whereis -m printf
- printf: /usr/share/man/man1/printf.1.gz /usr/share/man/man3/printf.3.gz
- //获得了manual文件那个是C语言中printf函数的呢?一般地较大的是。下面逐个验证
- wzb55@ubuntu:~/c$ man 1 printf //user command 发现是Shell命令的printf
- wzb55@ubuntu:~/c$ man 3 printf //Linux programer's Manual
- wzb55@ubuntu:~/c$
whereis 是不是对C编程很有用呀。
二、下面说一下 which命令吧!
- wzb55@ubuntu:~/c$ man which
- WHICH(1) WHICH(1)
- NAME //命令名--功能简介
- which - locate a command //定位一个命令
- SYNOPSIS //用法大纲
- which [-a] filename ...
- DESCRIPTION //功能的详细描述
- which returns the pathnames of the files which would be executed in the
- current environment, 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.
- //which 返回符合POSIX标准的命令的可执行路径
- OPTIONS //命令的选项说明
- -a print all matching pathnames of each argument
- //为每一个参数命令打印其所有的匹配路径
- EXIT STATUS //返回状态 在Linux下可以用 echo $? 查看一个执行完的程序的返回状态
- 0 if all specified commands are found and executable
- //所有指定命令都找到了可执行的路径
- 1 if one or more specified commands is nonexistent or not exe-
- cutable
- //有一个或多于一个参数命令,是不存在或不可执行的,即找不到其路径
- 2 if an invalid option is specified // 指定了一个非法选项
- Debian 12 Jul 2004 WHICH(1)
可见which 与whereis -b 功能有相似之处:同样在编写Shell脚本时可以快速查找命令的绝对路径;
看下面which应用的举例:
- wzb55@ubuntu:~/c$
- wzb55@ubuntu:~/c$ which time
- /usr/bin/time
- wzb55@ubuntu:~/c$ which -a time
- /usr/bin/time
- /usr/bin/X11/time
- wzb55@ubuntu:~/c$ echo $?
- 0
- wzb55@ubuntu:~/c$ which ls
- /bin/ls
- wzb55@ubuntu:~/c$ which -a ls
- /bin/ls
- wzb55@ubuntu:~/c$ echo $?
- 0
- wzb55@ubuntu:~/c$ which printff
- wzb55@ubuntu:~/c$ echo $?
- 1
- wzb55@ubuntu:~/c$ which -ap ls
- Illegal option -p
- Usage: /usr/bin/which [-a] args
- wzb55@ubuntu:~/c$ echo $?
- 2
- wzb55@ubuntu:~/c$
至此,学习了which ,whereis ,man 的一部分小功能,在Linux下进行C编程时,对于那些复杂的函数的参数,用法的记忆,就不怕啦,每次想看时,就man一下,瞅一眼,慢慢的就熟喽!