天天看点

文件搜索locate,which ,whereis ,grep

文件搜索locate、which、whereis、grep

文章目录

    • 文件搜索locate、which、whereis、grep
      • 1 locate
          • 语法:
          • 优点:
          • 缺点:
      • 2 which
      • 3 whereis
      • 4 grep

视频链接

1 locate

语法:
locate   文件名名称  
locate   -i   文件名称 忽略大小写
返回绝对路径
           
优点:

在文件资料库 /var/lib/mlocate/mlocate.db 中搜索,耗费系统资源少,适合快速搜索配置文件,系统文件

这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。整个locate工作其实是由四部分组成的:
  1. /usr/bin/updatedb 主要用来更新数据库,通过crontab自动完成的
  2. /usr/bin/locate 查询文件位置
  3. /etc/updatedb.conf updatedb的配置文件
  4. /var/lib/mlocate/mlocate.db 存放文件信息的文件
缺点:

(1)不能实时搜索(find 是实时搜索),必须更新文件资料库后才能找到刚建立的文件

解决方法:updatedb

(2)有些目录不在文件资料库的搜索范围内,比如/tmp临时文件目录

2 which

which 命令名称

显示命令所在目录及别名信息

3 whereis

whereis 命令名称

显示命令所在目录及帮助文档位置

4 grep

查找文件里面的内容

语法:grep   搜索得字符串   文件位置

查找/etc/ininttab中multiuser 中所在得行
grep     multiuser /etc/inittab

不区分大小写
grep     -i    multiuser   /etc/inittab

屏蔽以#开头的行   反向查找
grep     -v    ^#     /etc/inittab
           

只查看yum.conf中非注释的行

[[email protected] etc]# grep  -v  ^#  yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

           

继续阅读