linux文件操纵工具:定位文件的下令locate详解

打印 上一主题 下一主题

主题 882|帖子 882|积分 2646

目录
一、概述
二、工作原理
三、安装
1、在 Debian 或 Ubuntu 上:
2、在 Fedora 或 CentOS 上:
(1)下令
(2)执行效果
四、更新索引
1、首次使用
2、更新索引后的效果
五、根本用法
1、根本语法
2、选项
3、帮助获取
六、示例
1、忽略巨细写查找文件 
2、使用正则表达式查找以 .txt 结尾的文件
3、输出效果以 null 字符终止
七、联合其他下令使用
八、其他事项


一、概述

        locate 下令是在 Linux 和类 Unix 体系中快速查找文件路径的强大工具。与 find 下令相比,locate 的速率通常更快,因为它不直接扫描文件体系,而是查询一个预先构建的索引数据库。

二、工作原理

        locate 下令依赖于一个名为 mlocate 的步伐来维护索引数据库。该数据库通常每天通过 cron 作业自动更新一次。这意味着 locate 可能不会立刻反映文件体系的最新更改。

三、安装

大多数 Linux 发行版都默认安装了 mlocate。如果没有安装,你可以通过包管理器安装它。
1、在 Debian 或 Ubuntu 上:

sudo apt-get install mlocate

2、在 Fedora 或 CentOS 上:

(1)下令

           sudo dnf install mlocate
  (2)执行效果

        执行上述下令后,效果如下:
  1. [root@ecs-52a1 /]#
  2. [root@ecs-52a1 /]# yum install mlocate
  3. Loaded plugins: fastestmirror
  4. Loading mirror speeds from cached hostfile
  5. * base: mirrors.aliyun.com
  6. * extras: mirrors.aliyun.com
  7. * updates: mirrors.aliyun.com
  8. Resolving Dependencies
  9. --> Running transaction check
  10. ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed
  11. --> Finished Dependency Resolution
  12. Dependencies Resolved
  13. ==============================================================================================================================================================
  14. Package                               Arch                                 Version                                  Repository                          Size
  15. ==============================================================================================================================================================
  16. Installing:
  17. mlocate                               x86_64                               0.26-8.el7                               base                               113 k
  18. Transaction Summary
  19. ==============================================================================================================================================================
  20. Install  1 Package
  21. Total download size: 113 k
  22. Installed size: 379 k
  23. Is this ok [y/d/N]: y
  24. Downloading packages:
  25. mlocate-0.26-8.el7.x86_64.rpm                                                                                                          | 113 kB  00:00:00
  26. Running transaction check
  27. Running transaction test
  28. Transaction test succeeded
  29. Running transaction
  30.   Installing : mlocate-0.26-8.el7.x86_64                                                                                                                  1/1
  31.   Verifying  : mlocate-0.26-8.el7.x86_64                                                                                                                  1/1
  32. Installed:
  33.   mlocate.x86_64 0:0.26-8.el7
  34. Complete!
  35. [root@ecs-52a1 /]# locate --h
  36. Usage: locate [OPTION]... [PATTERN]...
  37. Search for entries in a mlocate database.
  38.   -A, --all              only print entries that match all patterns
  39.   -b, --basename         match only the base name of path names
  40.   -c, --count            only print number of found entries
  41.   -d, --database DBPATH  use DBPATH instead of default database (which is
  42.                          /var/lib/mlocate/mlocate.db)
  43.   -e, --existing         only print entries for currently existing files
  44.   -L, --follow           follow trailing symbolic links when checking file
  45.                          existence (default)
  46.   -h, --help             print this help
  47.   -i, --ignore-case      ignore case distinctions when matching patterns
  48.   -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries
  49.   -m, --mmap             ignored, for backward compatibility
  50.   -P, --nofollow, -H     don't follow trailing symbolic links when checking file
  51.                          existence
  52.   -0, --null             separate entries with NUL on output
  53.   -S, --statistics       don't search for entries, print statistics about each
  54.                          used database
  55.   -q, --quiet            report no error messages about reading databases
  56.   -r, --regexp REGEXP    search for basic regexp REGEXP instead of patterns
  57.       --regex            patterns are extended regexps
  58.   -s, --stdio            ignored, for backward compatibility
  59.   -V, --version          print version information
  60.   -w, --wholename        match whole path name (default)
  61. Report bugs to mitr@redhat.com.
  62. [root@ecs-52a1 /]#
  63. [root@ecs-52a1 /]#
复制代码

四、更新索引

1、首次使用

        首次使用 locate 之前,或者当你必要更新索引数据库时,可以运行以下下令:
           sudo updated
          否则,当执行下令的时候,会出现如下错误:

2、更新索引后的效果

更新索引后,可以很快查到必要的文件,如下图所示:


五、根本用法

1、根本语法

           locate [OPTION]... [PATTERN]...
          查找包罗特定字符串的文件名
        如果想查找包罗字符串 example 的所有文件,可以使用:
           locate example
  
2、选项

        locate 下令支持一些选项来调整其行为,包括:
           - -i: 忽略巨细写差异。
          - -r: 使用正则表达式进行匹配。
          - -0: 输出效果以 null 字符终止,这对于包罗特殊字符的文件名特别有用。
          - -b: 匹配词边界,确保匹配的是完整的单词。
          - -d: 指定使用的数据库文件。
          - -c: 显示匹配项的数量,但不显示实际文件名。
  
3、帮助获取

       在下令行中输入指令:
           locate --h
         出现如下图所示的帮助信息:

六、示例

1、忽略巨细写查找文件 

             locate -i example.txt
   
2、使用正则表达式查找以 .txt 结尾的文件

            locate -r '\.txt$'
   
3、输出效果以 null 字符终止

  查找包罗 example 的文件名,并输出效果以 null 字符终止
             locate -0 example
   
七、联合其他下令使用

        locate 下令经常与其他下令一起使用来执行更复杂的任务。
        例如,可以使用 xargs 下令来处理 locate 的输出效果,查找所有 .txt 文件,并删除它们,使用如下下令:
       locate .txt | xargs rm
          注意,在执行类似上述删除操纵时要格外鉴戒,因为这可能会导致误删重要文件。
        
八、其他事项

        (1)locate 不会实时更新索引,因此可能无法找到近来修改或创建的文件。
        (2)由于 locate 速率快,它非常得当用于脚本中。
       (3)locate 是一个非常有用的下令,用于快速查找文件路径。虽然它不总是提供即时的效果,但在一样平常使用中非常方便。如果你必要查找文件体系的实时状态,可以考虑使用 find 下令。


文章正下方可以看到我的联系方式:鼠标“点击” 下面的 “威迪斯特-就是video system 微信名片”字样,就会出现我的二维码,欢迎沟通探讨。



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表