Linux 文件管理 : locate & slocate 命令详解

 

locate命令用于查找文件,Locate可以很快速的搜寻档案系统内是否有指定的档案。其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了。所以它需要一个数据库(默认:mlocate.db),一个配置文件(updatedb.conf),一个crontab(定时执行) 

 

  • 先运行updatedb, 可直接命令执行,也可以放在crontab里面执行
  • 然后在生成的/var/lib/mlocate/mlocate.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版本信息-V

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled- by-O-E-M:~$ locate -V
  2.  
    mlocate 0.25
  3.  
    Copyright (C) 2007 Red Hat, Inc. 保留所有权利。
  4.  
    本软件在 GPL v .2 下发布。
  5.  
     
  6.  
    本程序不提供任何担保,在法法律允许的范围内予以排除。

 

#查看locate帮助信息

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -h
  2.  
    用法: locate [OPTION]... [PATTERN]...
  3.  
    在mlocate数据库中搜索条目.
  4.  
     
  5.  
    -b, --basename 匹配唯一的路径名称的基本文件名
  6.  
    -c, --count 只显示找到条目的号码
  7.  
    -d, --database DBPATH 用 DBPATH 替代默认的数据库(/var/lib/mlocate/mlocate.db)
  8.  
    -e, --existing 只显示当前存在的文件条目
  9.  
    -L, --follow 当文件存在时跟随蔓延的符号链接 (默认)
  10.  
    -h, --help 显示本帮助
  11.  
    -i, --ignore-case 匹配模式时忽略大小写区别
  12.  
    -l, --limit, -n LIMIT 限制为 LIMIT项目的输出 (或 计数)
  13.  
    -m, --mmap 忽略向后兼容性
  14.  
    -P, --nofollow, -H 当检查文件时不跟随蔓延的符号
  15.  
    链接
  16.  
    -0, --null 输出时以 NUL 分隔项目
  17.  
    -S, --statistics 不搜索项目,显示有关每个已用数据库的统计信息
  18.  
    -q, --quiet 不报告关于读取数据库的错误消息
  19.  
    -r, --regexp REGEXP 搜索基本正则表达式 REGEXP 来代替模式
  20.  
    --regex 模式是扩展正则表达式
  21.  
    -s, --stdio 忽略向后兼容性
  22.  
    -V, --version 显示版本信息
  23.  
    -w, --wholename 匹配完整路径名 (默认)
  24.  
     
  25.  
    将 bug 报告给 mitr@redhat.com.

 

# 查找etc目录下所有以sh开头的文件

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled-by-O-E- M:~$ locate /etc/sh
  2.  
    /etc/shadow
  3.  
    /etc/shadow-
  4.  
    /etc/shells

 

 

 

 

 

#查看soft目录下以android开头的文件,限定只显示5个,使用 -n

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled- by-O-E-M:~$ locate -n 5 /soft/android
  2.  
    /home/bixiaopeng/soft/android-studio
  3.  
    /home/bixiaopeng/soft/android-studio-bundle -130.737825-linux.tgz
  4.  
    /home/bixiaopeng/soft/android-studio/Install-Linux-tar.txt
  5.  
    /home/bixiaopeng/soft/android-studio/LICENSE.txt
  6.  
    /home/bixiaopeng/soft/android-studio/NOTICE.txt

 

#查找时区分大小写

 

 
  1.  
    bixiaopeng@bixiaopeng- To-be-filled- by-O-E-M:~$ locate makefile
  2.  
    /usr/share/doc/python3-uno/demo/makefile.mk
  3.  
    /usr/share/emacs/ 23.4/lisp/cedet/ede/makefile-edit.elc
  4.  
    /usr/share/gtksourceview -3.0/language-specs/makefile.lang
  5.  
    /usr/share/icons/Humanity/mimes/ 16/gnome-mime- text-x-makefile.svg
  6.  
    /usr/share/icons/Humanity/mimes/ 22/gnome-mime- text-x-makefile.svg
  7.  
     
  8.  
    .....

 

 

 

#查找时不区分大小写,使用 -i

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled- by-O-E-M:~$ locate -i makefile
  2.  
     
  3.  
    /usr/src/linux-headers -3.8 .0 -30/kernel/Makefile
  4.  
    /usr/src/linux-headers -3.8 .0 -30/kernel/debug/Makefile
  5.  
    /usr/src/linux-headers -3.8 .0 -30/kernel/debug/kdb/Makefile
  6.  
    /usr/src/linux-headers -3.8 .0 -30/kernel/events/Makefile
  7.  
     
  8.  
    ......

 

# ------------- 使用正规运算式 做查找的条件 locate -r

#查找以makefile结尾的文件

 

 
  1.  
    bixiaopeng@bixiaopeng- To-be-filled-by-O-E-M:~$ locate -r makefile$
  2.  
    /usr/src/linux-headers- 3.8. 0- 19/scripts/mkmakefile
  3.  
    /usr/src/linux-headers- 3.8. 0- 19- generic/scripts/mkmakefile
  4.  
    /usr/src/linux-headers- 3.8. 0- 27/scripts/mkmakefile
  5.  
    /usr/src/linux-headers- 3.8. 0- 27- generic/scripts/mkmakefile
  6.  
    /usr/src/linux-headers- 3.8. 0- 29/scripts/mkmakefile
  7.  
    /usr/src/linux-headers- 3.8. 0- 29- generic/scripts/mkmakefile
  8.  
    /usr/src/linux-headers- 3.8. 0- 30/scripts/mkmakefile
  9.  
    /usr/src/linux-headers- 3.8. 0- 30- generic/scripts/mkmakefile

 

#查找以/var/lib/dpkg/info/开头的文件

 

 
  1.  
    bixiaopeng@bixiaopeng-To-be-filled- by-O-E-M:~$ locate -r ^/ var/lib/dpkg/info/
  2.  
     
  3.  
    / var/lib/dpkg/info/zlib1g:amd64.postinst
  4.  
    / var/lib/dpkg/info/zlib1g:amd64.postrm
  5.  
    / var/lib/dpkg/info/zlib1g:amd64.shlibs
  6.  
    / var/lib/dpkg/info/zlib1g:amd64.symbols
  7.  
    / var/lib/dpkg/info/zlib1g:i386.list
  8.  
    / var/lib/dpkg/info/zlib1g:i386.md5sums
  9.  
    / var/lib/dpkg/info/zlib1g:i386.postinst
  10.  
    / var/lib/dpkg/info/zlib1g:i386.postrm
  11.  
    / var/lib/dpkg/info/zlib1g:i386.shlibs
  12.  

 

# locate -S           //-->查看统计信息,目录,文件,比特数适用情况

 

 
  1.  
    Database / var/lib/mlocate/mlocate.db:
  2.  
    3, 315 directories
  3.  
    37, 228 files
  4.  
    1, 504, 439 bytes in file names
  5.  
    594, 851 bytes used to store database

# locate -c httpd.conf     //-->统计查找到的数量 

3

# cat /etc/updatedb.conf   //-->配置文件

 
  1.  
    PRUNE_BIND_MOUNTS = "yes"
  2.  
    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"
  3.  
    PRUNENAMES = ".git .hg .svn"
  4.  
    PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache
  5.  
    /var/spool/cups /var/spool/squid /var/tmp"
  6.  
    PRUNEPATHS= "/efs /efsroots"
  7.  
    export PRUNEPATHS

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HarkerYX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值