1. 程式人生 > >【轉】locate命令簡介

【轉】locate命令簡介

1、命令簡介

        locate(locate) 命令用來查詢檔案或目錄。 locate命令要比find -name快得多,原因在於它不搜尋具體目錄,而是搜尋一個數據庫/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  存放檔案資訊的檔案

2、用法

locate [OPTION]... [PATTERN]...

3、選項

-b, –basename

match only the base name of path names
-c, –count 只輸出找到的數量
-d, --database DBPATH 使用DBPATH指定的資料庫,而不是預設資料庫 /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 顯示幫助
-i, --ignore-case
忽略大小寫
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H dont follow trailing symbolic links when checking file existence
-0, –null separate entries with NUL on output
-S, --statistics dont search for entries, print statistics about eachused database
-q, –quiet 安靜模式,不會顯示任何錯誤訊息
-r, –regexp REGEXP 使用基本正則表示式
regex 使用擴充套件正則表示式
-s, --stdio ignored, for backward compatibility
-V, –version 顯示版本資訊
-w, --wholename match whole path name (default)

4、示例

示例1: 搜尋etc目錄下所有以my開頭的檔案
[[email protected] lib]#locate  /etc/my
/etc/my.cnf
示例2:新增的檔案無法locate,使用updatedb
touch new.txt
[[email protected] ~]# locate new.txt
[[email protected] ~]# updatedb
[[email protected] ~]# locate new.txt
/root/new.txt
示例3:updatedb的配置檔案/etc/updatedb.conf
 [[email protected] ~]# cat /etc/updatedb.conf 
PRUNE_BIND_MOUNTS = yes
PRUNEFS = 9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs
PRUNENAMES = .git .hg .svn
PRUNEPATHS = /afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp

第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否進行限制搜尋。

第二行是排除檢索的檔案系統型別,即列出的檔案系統型別不進行檢索。

第二行表示對哪些字尾的檔案排除檢索,也就是列在這裡面的字尾的檔案跳過不進行檢索。不同字尾之間用空格隔開。

第四行是排除檢索的路徑,即列出的路徑下的檔案和子資料夾均跳過不進行檢索。updatedb之後使用locate仍然找不到想要檔案

可以檢查掛載的目錄是否被忽略了