首页 > 代码库 > locate简介及用法

locate简介及用法

简介

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.2
Copyright (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