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 ~]# 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM