3.8 locate文件数据库中查找

3.8.1语法

locate [OPTION]… [PATTERN]…

  • locate可以很快的搜寻档案系统中是否有指定档案,其方法时先建立一个包括系统内所有档案名称及路径的索引数据库。寻找时,只需检索索引数据库,不必深入档案系统之中。locate因为只去检索素菊开,所有比find全盘检索要快很多。
  • 在一般的distribution中,数据库建立都被放在crontab中自动执行。由于locate使用的数据库是通过crondaemon周期性建立来更新的,一般是一周更新一次,所以在今天内新建的文件会查找不到,或者查找到已经删除了的文件。那么可以通过updatedb命令手动更新数据库,或者质检再重新调整更新周期

特点总结

  • 非实时,模糊匹配,查找是根据全系统文件数据库进行的
  • 如果文件数据库不存在,则不能查找
  • updatedb,手动生成文件数据库
  • 查找速度快
  • 例子:【locate xdd -i】查找包含xdd的文件目录
    注意:locate不指定参数的情况下默认是从根目录开始查找 –i查找时不区分大小写
  • locate文件查找(在文件数据库中查找)_locate在数据库中查找

  • 例子:【locate –ic xdd】查找包含xdd的文件目录,不区分大小写,只显示找到了多少条数目
  • locate文件查找(在文件数据库中查找)_locate在数据库中查找_02

3.8.2常用参数

选项 说明

  • -A, --all 只显示匹配所有模式的条目
  • -b, --basename 只匹配路径名的基本名称
  • -c, --count 只显示找到的条目的数目
  • -d, --database DBPATH 使用 DBPATH,而非默认数据(即:/var/lib/mlocate/mlocate.db)
  • -e, --existing 只显示当前存在的文件条目
  • -L, --follow 当文件存在时跟随蔓延的符号链接(默认)
  • -h, --help 显示帮助信息
  • -i, --ignore-case 匹配模式时忽略大小写区别
  • -l, --limit, -n LIMIT 限制为 LIMIT项目的输出(或 计数)
  • -m, --mmap 被忽略,用于向后兼容
  • -P, --nofollow, -H 当检查文件时不跟随蔓延的符号链接
  • -0, --null 输出时以 NUL 分隔项目
  • -S, --statistics 不搜索项目,显示有关每个已用数据库的统计信息
  • -q, --quiet 不报告关于读取数据库的错误消息
  • -r, --regexp REGEXP 搜索基本正则表达式 REGEXP 而不是模式
  • –regex 模式是扩展正则表达式
  • -s, --stdio 被忽略,用于向后兼容
  • -V, --version 显示版本信息
  • -w, --wholename 匹配完整路径名(默认)

3.8.3附加(–help)

[root@gdy103 ~]# locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.

-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)

将 bug 报告给 mitr@redhat.com.
[root@gdy103 ~]#