Linux 命令locate

原文:https://blog.csdn.net/liang19890820/article/details/53285624

简述

locate 可以很快速的搜寻档案系统内是否有指定的档案。其方法是先建立一个包括系统内所有档案名称及路径的索引数据库,当寻找时就只需查询这个索引数据库而不必实际深入档案系统之中。locate 因为是只去检索索引数据库,所以在比 find 全盘检索要快很多。

在一般的 distribution 之中,数据库的建立都被放在 crontab 中自动执行。由于 locate 使用的数据库是通过 crondaemon 周期性建立来更新的。一般是一周更新一次,所以在近今天内新建的文件会查找不到,或者会查找到已经删除了的文件。那么,可以通过 updatedb 命令手动更新数据库,或者自己再重新调整更新周期。

 

 

命令介绍

  • 命令名称 
    locate

  • 基本语法 
    locate [OPTION]… [PATTERN]…

  • 功能描述 
    在 mlocate 数据库中搜索条目

命令选项

选项说明
-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 匹配完整路径名(默认)

使用范例

1.搜索目录下所有以 sh 开头的文件

[wang@localhost ~]$ locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
 

2.查找包含某个字符串的相关文件

[wang@localhost ~]$ locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/kpasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
# 部分显示结果省略
/usr/share/man/man8/chpasswd.8.gz
/usr/share/man/man8/saslpasswd2.8.gz
/usr/share/man/man8/smbpasswd.8.gz
/usr/share/man/pt_BR/man1/gpasswd.1.gz
/usr/share/man/ru/man1/gpasswd.1.gz
/usr/share/man/ru/man8/chpasswd.8.gz
/usr/share/man/zh_CN/man1/gpasswd.1.gz
/usr/share/man/zh_CN/man8/chpasswd.8.gz
/usr/share/man/zh_TW/man8/chpasswd.8.gz
/usr/share/vim/vim74/ftplugin/passwd.vim
/usr/share/vim/vim74/syntax/passwd.vim
 

3.限定显示数量

如果显示的内容过多,可以使用 -n 选项来限定显示数量。

[wang@localhost ~]$ locate -n 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
 

4.查找时不区分大小写

当查找不需要区分大小写时,使用 -i 选项。

[wang@localhost ~]$ locate -i -n 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
 

5.使用正则表达式

当需要查找符合特定规则的信息时,可以使用 -r 选项匹配相应的正则表达式。

# 查找以 /var/lib/rpm 开头的文件
[wang@localhost ~]$ locate -r ^/var/lib/rpm
/var/lib/rpm
/var/lib/rpm-state
/var/lib/rpm/.dbenv.lock
/var/lib/rpm/.rpm.lock
/var/lib/rpm/Basenames
/var/lib/rpm/Conflictname
/var/lib/rpm/Dirnames
/var/lib/rpm/Group
/var/lib/rpm/Installtid
/var/lib/rpm/Name
/var/lib/rpm/Obsoletename
/var/lib/rpm/Packages
/var/lib/rpm/Providename
/var/lib/rpm/Requirename
/var/lib/rpm/Sha1header
/var/lib/rpm/Sigmd5
/var/lib/rpm/Triggername
/var/lib/rpm/__db.001
/var/lib/rpm/__db.002
/var/lib/rpm/__db.003
/var/lib/rpm-state/gconf
# 查找以 zip 结尾的文件
[wang@localhost ~]$ locate -r zip$
/home/wang/doc.zip
/usr/bin/funzip
/usr/bin/gpg-zip
/usr/bin/gunzip
/usr/bin/gzip
/usr/bin/mzip
/usr/bin/unzip
/usr/bin/zip
/usr/lib64/libreoffice/share/config/images.zip
/usr/lib64/libreoffice/share/config/images_crystal.zip
/usr/lib64/libreoffice/share/config/images_galaxy.zip
/usr/lib64/libreoffice/share/config/images_hicontrast.zip
/usr/lib64/libreoffice/share/config/images_oxygen.zip
/usr/lib64/libreoffice/share/config/images_sifr.zip
/usr/lib64/libreoffice/share/config/images_tango.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-blue.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-green.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-red.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-gorilla.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-white.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/simple.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-blue.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-gray.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-green.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-red.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-yellow.zip
/usr/local/lib/python3.5/test/zip_cp437_header.zip
/usr/local/lib/python3.5/test/zipdir.zip
/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/missing_directory.zip
/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/nested_portion1.zip
/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/top_level_portion1.zip
/usr/share/bash-completion/completions/gzip
 

6.查找最近变动过的文件

使用 locate 命令查不到最近变动过的文件。为了避免这种情况,可以在使用 locate 之前,先使用 updatedb 命令,手动更新数据库。

# 新建文件
[root@localhost ~]# touch new_file
[root@localhost ~]# locate ~/new_file
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/new_file
/root/new_file
[root@localhost ~]# 
# 删除文件
[root@localhost ~]# rm -rf file.txt 
[root@localhost ~]# locate ~/file.txt
/root/file.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/file.txt
[root@localhost ~]# 
 
posted @ 2018-03-30 12:19  奋斗终生  Views(5608)  Comments(0Edit  收藏  举报