Linux系统文件查找详解

今天给伙伴们分享一下Linux文件查找详解,希望看了有所收获。

我是公众号「想吃西红柿」「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!

如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。

一、which

  • shows the full path of (shell) commands.

  • 显示(系统)命令所在目录

[[email protected] ~]# which ls
alias ls='ls --color=auto'
	/bin/ls
[[email protected] ~]# which ll
alias ll='ls -l --color=auto'
	/bin/ls
[[email protected] ~]# which sshd
/usr/sbin/sshd
[[email protected] ~]# which mysqld
/usr/sbin/mysqld
[[email protected] ~]# which mysql
/usr/bin/mysql

二、whereis

  • locate the binary, source, and manual page files for a command

  • 除了可查看此命令所在的路径,还可以查看此文件相关的帮助文档所在路径,命令原始路径,命令配置文件(如果有) 帮助文件(如果有)

[[email protected] ~]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
[[email protected] ~]# whereis sshd
sshd: /usr/sbin/sshd /usr/share/man/man8/sshd.8
[[email protected] ~]# whereis mysqld
mysqld: /usr/sbin/mysqld /usr/share/man/man8/mysqld.8.gz
[[email protected] ~]# whereis which
which: /usr/bin/which /usr/share/man/man1/which.1.gz

三、whatis

  • 解释命令的意义/功能;获得此命令的索引的简短说明信息

  • 备注:许多Linux系统中并未默认安装此软件,需手动下载、安装。

  • 可等效于man / --help

[[email protected] ~]# whatis cp 
cp (1)               - 复制文件和目录
cp (1p)              - copy files
[[email protected] ~]# whatis service 
service (8)          - run a System V init script
[[email protected] ~]# whatis systemctl 
systemctl (1)        - Control the systemd system and service manager

出错显示:

[[email protected] ~]# whatis cp 
cp:没有 appropriate。

原因:

  • whatis是根据数据库执行查找操作的
  • 数据库是定是跟新的,但新安装的系统还未自动跟新

方法:

  • root权限下
  • 使用makewhatis手动更新(OS7版本以下)
  • 使用mandb进行更新(OS7及其以上)

四、locate

  • locate 指令和 find 找寻档案的功能类似,但 locate 是透过 update 程序将硬盘中的所有文件和目录资料先建立一个索引数据库,在执行 loacte 时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接下达update强迫系统立即修改索引数据库。
locate:依赖于事先构建好的索引库
	1、系统自动实现(周期性任务)
	2、手动更新数据库(updatedb)

locate 工作特性:
	1、查找速度快
	2、模糊查找
	3、非实时查找
1、locate 命令格式
locate  [OPTION]...  PATTERN...
	-b:只匹配路径中的基名
	-c:统计出共有多少个符合条件的文件
	-r:BRE,使用正规运算式 做寻找的条件
[[email protected] ~]# locate 被查找的关键字
注意:索引构建过程需要遍历整个根文件系统,极消耗资源。
2、命令示例
示例:
[[email protected] ~]# locate /etc/sh       # 查找 /etc 下以 sh 开头的文件
/etc/shadow
/etc/shadow-
/etc/shells
[

你可能感兴趣的:(Linux,操作系统管理,linux,运维,服务器,云计算)