標籤:linux
簡介
locate命令用於尋找檔案,Locate可以很快速的搜尋檔案系統內是否有指定的檔案。其方法是先建立一個包括系統內所有設定檔名稱及路徑的資料庫,之後當尋找時就只需查詢這個資料庫,而不必實際深入檔案系統之中了。所以它需要一個資料庫(預設:slocate.db),一個設定檔(updatedb.conf),一個crontab(定時執行)
- 先運行updatedb, 可直接命令執行,也可以放在crontab裡面執行
- 然後在產生的/var/lib/slocate/slocate.db資料庫中尋找。
所以
- locate的速度比find快,因為它並不是真的尋找檔案,而是查資料庫
- locate的尋找並不是即時的,而是以資料庫的更新為準,一般是系統自己維護.
- locate的升級資料庫命令:locate -- u #注意之間有空格
# ls -lart mlocate.db #更新前
-rw-r----- 1 root slocate 1286901 Aug 8 03:23 mlocate.db
# updatedb
# ls -lart mlocate.db #更新後
-rw-r----- 1 root slocate 1296268 Aug 8 11:10 mlocate.db
用法
# locate -S //-->查看統計資訊,目錄,檔案,位元數適用情況
Database /var/lib/mlocate/mlocate.db: 3,315 directories 37,228 files 1,504,439 bytes in file names 594,851 bytes used to store database
# locate -V //-->查看版本資訊
<pre name="code" class="plain">mlocate 0.22.2Copyright (C) 2007 Red Hat, Inc. All rights reserved.This software is distributed under the GPL v.2.This program is provided with NO WARRANTY, to the extent permitted by law.
# locate -h //-->查看協助資訊
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)
# locate /etc/sh //-->尋找/etc/sh*
/etc/shadow/etc/shadow-/etc/shadow.bup/etc/shadow.bup.20140725-214324/etc/shells
# locate -n 5 apache //-->只找帶有apache目錄,顯示其中5行
/etc/selinux/targeted/modules/active/modules/apache.pp/opt/cpf/oma/3rdparty/apache-tomcat/opt/cpf/oma/3rdparty/apache-tomcat/LICENSE/opt/cpf/oma/3rdparty/apache-tomcat/NOTICE/opt/cpf/oma/3rdparty/apache-tomcat/RELEASE-NOTES
# locate -r makefile$ //-->尋找結尾為makefile的檔案目錄
/usr/share/doc/bcel-5.2/verifier/GNUmakefile
# locate -n 3 -r ^\/opt //-->尋找以/opt開頭的目錄,且只顯示3行
/opt/opt/cpf/opt/oss
#locate -i /HTTP //-->尋找/HTTP目錄,忽略大小寫
/usr/share/perl5/Net/HTTP/NB.pm/usr/share/perl5/URI/http.pm/usr/share/perl5/URI/https.pm
# locate -c httpd.conf //-->統計尋找到的數量
3
# 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 efs100 lofs"PRUNENAMES = ".git .hg .svn"PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache/var/spool/cups /var/spool/squid /var/tmp"PRUNEPATHS="/efs /efsroots"export PRUNEPATHS