Linux 命令locate
原文:https://blog.csdn.net/liang19890820/article/details/53285624
简述
locate 可以很快速的搜寻档案系统内是否有指定的档案。其方法是先建立一个包括系统内所有档案名称及路径的索引数据库,当寻找时就只需查询这个索引数据库而不必实际深入档案系统之中。locate 因为是只去检索索引数据库,所以在比 find 全盘检索要快很多。
在一般的 distribution 之中,数据库的建立都被放在 crontab 中自动执行。由于 locate 使用的数据库是通过 crondaemon 周期性建立来更新的。一般是一周更新一次,所以在近今天内新建的文件会查找不到,或者会查找到已经删除了的文件。那么,可以通过 updatedb 命令手动更新数据库,或者自己再重新调整更新周期。
命令介绍
命令名称
locate基本语法
locate [OPTION]… [PATTERN]…功能描述
在 mlocate 数据库中搜索条目
命令选项
| 选项 | 说明 |
|---|---|
-A, --all |
只显示匹配所有模式的条目 |
-b, --basename |
只匹配路径名的基本名称 |
-c, --count |
只显示找到的条目的数目 |
-d, --database DBPATH |
使用 DBPATH,而非默认数据(即:/var/lib/mlocate/mlocate.db) |
-e, --existing |
只显示当前存在的文件条目 |
-L, --follow |
当文件存在时跟随蔓延的符号链接(默认) |
-h, --help |
显示帮助信息 |
-i, --ignore-case |
匹配模式时忽略大小写区别 |
-l, --limit, -n LIMIT |
限制为 LIMIT项目的输出(或 计数) |
-m, --mmap |
被忽略,用于向后兼容 |
-P, --nofollow, -H |
当检查文件时不跟随蔓延的符号链接 |
-0, --null |
输出时以 NUL 分隔项目 |
-S, --statistics |
不搜索项目,显示有关每个已用数据库的统计信息 |
-q, --quiet |
不报告关于读取数据库的错误消息 |
-r, --regexp REGEXP |
搜索基本正则表达式 REGEXP 而不是模式 |
--regex |
模式是扩展正则表达式 |
-s, --stdio |
被忽略,用于向后兼容 |
-V, --version |
显示版本信息 |
-w, --wholename |
匹配完整路径名(默认) |
使用范例
1.搜索目录下所有以 sh 开头的文件
[wang@localhost ~]$ locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
2.查找包含某个字符串的相关文件
[wang@localhost ~]$ locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/kpasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
# 部分显示结果省略
/usr/share/man/man8/chpasswd..gz
/usr/share/man/man8/saslpasswd2..gz
/usr/share/man/man8/smbpasswd..gz
/usr/share/man/pt_BR/man1/gpasswd..gz
/usr/share/man/ru/man1/gpasswd..gz
/usr/share/man/ru/man8/chpasswd..gz
/usr/share/man/zh_CN/man1/gpasswd..gz
/usr/share/man/zh_CN/man8/chpasswd..gz
/usr/share/man/zh_TW/man8/chpasswd..gz
/usr/share/vim/vim74/ftplugin/passwd.vim
/usr/share/vim/vim74/syntax/passwd.vim
3.限定显示数量
如果显示的内容过多,可以使用 -n 选项来限定显示数量。
[wang@localhost ~]$ locate -n passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
4.查找时不区分大小写
当查找不需要区分大小写时,使用 -i 选项。
[wang@localhost ~]$ locate -i -n passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
5.使用正则表达式
当需要查找符合特定规则的信息时,可以使用 -r 选项匹配相应的正则表达式。
# 查找以 /var/lib/rpm 开头的文件
[wang@localhost ~]$ locate -r ^/var/lib/rpm
/var/lib/rpm
/var/lib/rpm-state
/var/lib/rpm/.dbenv.lock
/var/lib/rpm/.rpm.lock
/var/lib/rpm/Basenames
/var/lib/rpm/Conflictname
/var/lib/rpm/Dirnames
/var/lib/rpm/Group
/var/lib/rpm/Installtid
/var/lib/rpm/Name
/var/lib/rpm/Obsoletename
/var/lib/rpm/Packages
/var/lib/rpm/Providename
/var/lib/rpm/Requirename
/var/lib/rpm/Sha1header
/var/lib/rpm/Sigmd5
/var/lib/rpm/Triggername
/var/lib/rpm/__db.
/var/lib/rpm/__db.
/var/lib/rpm/__db.
/var/lib/rpm-state/gconf
# 查找以 zip 结尾的文件
[wang@localhost ~]$ locate -r zip$
/home/wang/doc.zip
/usr/bin/funzip
/usr/bin/gpg-zip
/usr/bin/gunzip
/usr/bin/gzip
/usr/bin/mzip
/usr/bin/unzip
/usr/bin/zip
/usr/lib64/libreoffice/share/config/images.zip
/usr/lib64/libreoffice/share/config/images_crystal.zip
/usr/lib64/libreoffice/share/config/images_galaxy.zip
/usr/lib64/libreoffice/share/config/images_hicontrast.zip
/usr/lib64/libreoffice/share/config/images_oxygen.zip
/usr/lib64/libreoffice/share/config/images_sifr.zip
/usr/lib64/libreoffice/share/config/images_tango.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-blue.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-green.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-red.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-gorilla.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-white.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/simple.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-blue.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-gray.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-green.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-red.zip
/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-yellow.zip
/usr/local/lib/python3./test/zip_cp437_header.zip
/usr/local/lib/python3./test/zipdir.zip
/usr/local/lib/python3./test/test_importlib/namespace_pkgs/missing_directory.zip
/usr/local/lib/python3./test/test_importlib/namespace_pkgs/nested_portion1.zip
/usr/local/lib/python3./test/test_importlib/namespace_pkgs/top_level_portion1.zip
/usr/share/bash-completion/completions/gzip
6.查找最近变动过的文件
使用 locate 命令查不到最近变动过的文件。为了避免这种情况,可以在使用 locate 之前,先使用 updatedb 命令,手动更新数据库。
# 新建文件
[root@localhost ~]# touch new_file
[root@localhost ~]# locate ~/new_file
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/new_file
/root/new_file
[root@localhost ~]#
# 删除文件
[root@localhost ~]# rm -rf file.txt
[root@localhost ~]# locate ~/file.txt
/root/file.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/file.txt
[root@localhost ~]#
Linux 命令locate的更多相关文章
- 每天一个linux命令--locate
linux下,不知道自己安装的程序放在哪里了,可以使用locate命令进行查找. [hongye@dev107 ~]$ locate activemq.xml /home/hongye/hongyeC ...
- linux命令:locate
1.命令简介 locate(locate) 命令用来查找文件或目录. locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/ml ...
- Linux命令locate
centos安装locate命令 centos6.3刚初始化安装完毕,有个配置文件不知道存在什么地方,想用locate命令来查找下,发现系统提示,找不到该命令.以前经常用的命令为什么找不到了呢???原 ...
- linux命令总结之查找命令find、locate、whereis、which、type
我们经常需要在系统中查找一个文件,那么在Linux系统中我们如何准确高效的确定一个文件在系统中的具体位置呢?一下我总结了在linux系统中用于查找文件的几个命令. 1.find命令 find是最常用也 ...
- Linux经常使用命令002之搜索命令locate、whereis、which、find、grep
Linux经常使用命令002之搜索命令locate.whereis.which.find.grep -20150811 经常使用搜索命令 -------文件搜索命令---------- -->l ...
- 每天一个Linux命令:locate(19)
locate locate命令 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中 ...
- Linux 命令中 which、whereis、locate 命令的用法
which 命令 which 命令的作用是,在 PATH 变量指定的路径中搜索可执行文件的所在位置.它一般用来确认系统中是否安装了指定的软件. (1)命令格式 which 可执行文件名称 wherei ...
- Linux系类(8) - 文件搜索命令locate
文件搜索命令locate 命令格式 locate [文件名] 在后台数据库中按文件名搜索,搜索速度更快,而find.which是遍历所有目录去查找:后台数据库在/var/lib/mlocate (保存 ...
- Linux文件搜索命令locate、which、grep详解
命令locate详解 命令locate,其基本功能是在文件资料库中可以快速的搜索系统文件,占用系统资源很少,例如:locate my.cnf 还可以使用locate -i [文件名],不区分大小写进行 ...
随机推荐
- C++ 智能指针一
/* 智能指针shared_ptr */ #include <iostream> #include <string> #include <memory> //智能指 ...
- 关于Solaris 的磁盘的分区
也许是深受LINUX Windows fdisk 影响,每次看完Solaris的format命令,总是云里雾里.我今天总结一下,各位给点指点 一. Linux.Windows 传统的磁盘区层级, ...
- Git错误:error: The following untracked working tree files would be overwritten by merge:
[andy@localhost weixin_robot]$ git pull Updating d652d1c..fa05549 error: The following untracked wor ...
- [Android Studio] Using NDK to call OpenCV
NDK才是Android开发通向超高薪之路.(这句话,似乎四年前有云) 难点在于常用的non-free module (sift and surf) unsw@unsw-UX303UB$ pwd /h ...
- Inside The C++ Object Model(一)
============================================================================1-1. 关键字struct和class的一点区 ...
- 【转】MYSQL-CLUSTER-7.5搭建数据库集群
阅读目录 前言 mysql cluster中的几个概念解释 架构图及说明 下载mysql cluster 安装mysql cluster之前 安装配置管理节点 安装配置数据和mysql节点 测试 启动 ...
- H - Painter
杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装.你现在需要使用这N种颜料:不但如此,你还需要一定数量的灰色颜料.杂货店从来不出售灰色颜料--也 ...
- 有重复行,查询时只保留最新一行的sql
一.表结构如下:表名test 二.sql select temp.* from (select test.*, row_number() over(partition by obd_code orde ...
- STL之map学习实例
``` #include<iostream> #include<algorithm> #include<vector> #include<map> #i ...
- 基础知识系列☞关键字→virtual
看到自己以前的一篇博客,感觉还差点什么,√,代码... using System; namespace Test { public class Program { private static voi ...