which 查找可执行文件(搜索命令所在路径及别名)
which命令在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。
使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令,-a选项将列出所有在PATH路径中可以找到的指令。
范例一 请找出 cd 这个指令的完整文件名
[root@localhost ~]# which -a cd
/usr/bin/cd
解析:cd是内部命令
范例二 用which查找which
[root@localhost ~]# which -a which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/alias
/usr/bin/which
/usr/bin/which
范例三 查找ifconfig指令的完整文件名
[root@localhost ~]# which -a ifconfig
/usr/sbin/ifconfig
whereis 查看文件的位置(搜索命令所在的路径以及帮助文档所在的位置)
whereis命令只能用于搜索程序名,而且只搜索二进制文件(选项-b)、man说明文件(选项-m)和源代码文件(选项-s)。
OPTION
-b 只查找可执行文件
-m 只查找帮助文件
-s 只查找源代码文件
如果省略参数,则返回所有信息
范例一 请用不同的身份找出ifconfig这个文档名
[root@localhost ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@localhost ~]# su - zhao
Last login: Tue Jan 21 17:51:24 CST 2020 on pts/0
[zhao@localhost ~]$ whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
范例二 只找出跟 passwd 有关的『说明文件』档名(man page)
[root@localhost ~]# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz
范例三 只找出passwd二进制文件
[root@localhost ~]# whereis -b passwd
passwd: /usr/bin/passwd /etc/passwd
find 实时查找工具
通过遍历指定起始路径下文件系统层级结构完成文件查找
工作特性 查找速度略慢、精确查找
locate
需要安装 yum install mlocate -y 先挂载设备
依赖于事先构建好的索引库
系统自动实现(周期性任务)
手动更新数据库(updatedb) locate数据库一天更新一次,创建的文件在没有做数据同步的时候是查找不到的
工作特性 查找速度快、模糊查找、非实时查找
locate [OPTION]... PATTERN...
OPTION
-b 只匹配路径中的基名
-c 统计出共有多少个符合条件的文件
-r BRE
注意:
①索引构建过程需要遍历整个根文件系统,极消耗资源
②查找文件的时候,要加绝对路径 locate /etc/passwd
范例 找出系统中所有passwd 相关的文件
locate passwd