locate簡介及用法

來源:互聯網
上載者:User

標籤: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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.