Linux 常用的查找命令find、locate、whereis、which、type。
- 对比
指令 | 示例 | 描述 |
---|---|---|
find | find ./ -name httpclient.zip | 遍历查找任何类型文件 |
locate | locate ./ httpclient.zip | 类似“find -name”,但由于是在本地数据库搜索,要比find更快 |
whereis | whereis -b ps | 只能搜索二进制文件(-b)、源代码文件(-s)、说明文件(-m) |
which | which ls | 只能在 PATH 变量指定的路径中搜索 |
type | type apt-get | 主要用于程序类型判断,shell内建程序,还是外部独立程序 |
- find
eric@eric-PC:~$ find ./ -name httpclient.zip
./Documents/linux-c/httpclient.zip
- locate
locate 是在本地数据库里面进行查找,所以会比find查找更快。但是由于本地数据库并不是实时更新,会导致新建的文件并不能用locate查找到,可以手动updatedb,进行更新数据库
eric@eric-PC:~$ sudo updatedb
/usr/bin/find: '/run/user/1000/gvfs': Permission denied
eric@eric-PC:~$ locate ./ httpclient.zip
/home/eric/Documents/linux-c/httpclient.zip
- whereis
该指令会在特定目录中查找符合条件的文件。这些文件应属于原始代码、二进制文件,或是帮助文件。
-b 只查找二进制文件
eric@eric-PC:~$ whereis -b ps
ps: /bin/ps
eric@eric-PC:~$
eric@eric-PC:~$ whereis -b arm-linux-gcc
arm-linux-gcc: /usr/local/arm/arm-linux-gcc-4.3.2/bin/arm-linux-gcc
eric@eric-PC:~$
- which
只能在系统PATH路径中查找,echo $PATH 可以查看系统PATH
eric@eric-PC:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin
eric@eric-PC:~$ which ls
/usr/bin/ls
- type
主要用来区分是shell内建程序,还是外部独立程序。如果是外部独立程序,则会提示对应的路径。
eric@eric-PC:~$ type cd
cd 是 shell 内建
eric@eric-PC:~$ type apt-get
apt-get 是 /usr/bin/apt-get
eric@eric-PC:~$