龙空技术网

Linux之locate命令

工业数字化 317

前言:

此时朋友们对“linux 查找命令 locate”大概比较关怀,朋友们都想要知道一些“linux 查找命令 locate”的相关文章。那么小编同时在网络上汇集了一些有关“linux 查找命令 locate””的相关内容,希望兄弟们能喜欢,你们一起来学习一下吧!

命令简介

locate(locate) 命令用来查找文件或目录。 locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/mlocate/mlocate.db 。这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,因此,我们在用whereis和locate查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。整个locate工作其实是由四部分组成的。

/usr/bin/updatedb 主要用来更新数据库,通过crontab自动完成的/usr/bin/locate 查询文件位置/etc/updatedb.conf updatedb的配置文件/var/lib/mlocate/mlocate.db 存放文件信息的文件如果系统中没有此命令

//Centos> yum install mlocate -y//Ubuntu> sudo apt-get install mlocate -y

初次使用需要执行updatedb,不然会出现locate: can not stat () /var/lib/mlocate/mlocate.db’: No such file or directory`

用法

locate [OPTION]... [PATTERN]...
命令参数-e 将排除在寻找的范围之外。-1 如果 是 1.则启动安全模式。在安全模式下,使用者不会看到权限无法看到 的档案。这会使速度减慢,因为 locate 必须至实际的档案系统中取得档案的 权限资料。-f 将特定的档案系统排除在外,例如我们没有必要把 proc 档案系统中的档案 放在资料库中。-q 安静模式,不会显示任何错误讯息。-n 至多显示 n个输出。-r 使用正规运算式 做寻找的条件。-o 指定资料库存的名称。-d 指定资料库的路径-h 显示辅助讯息-V 显示程式的版本讯息查找和pwd相关的所有文件
> locate pwd/etc/.pwd.lock/home/cpt/.local/share/fish/generated_completions/pwd.fish/home/cpt/.local/share/fish/generated_completions/pwdx.fish/root/.local/share/fish/generated_completions/pwd.fish/root/.local/share/fish/generated_completions/pwdx.fish/root/Python-3.6.4/Doc/library/pwd.rst/root/Python-3.6.4/Doc/library/spwd.rst/root/Python-3.6.4/Lib/test/test_pwd.py/root/Python-3.6.4/Lib/test/test_spwd.py/root/Python-3.6.4/Modules/pwdmodule.c/root/Python-3.6.4/Modules/pwdmodule.o/root/Python-3.6.4/Modules/spwdmodule.c/root/Python-3.6.4/Modules/clinic/pwdmodule.c.h/root/Python-3.6.4/Modules/clinic/spwdmodule.c.h/root/Python-3.6.4/build/lib.linux-x86_64-3.6/spwd.cpython-36m-x86_64-linux-gnu.so/root/Python-3.6.4/build/temp.linux-x86_64-3.6/root/Python-3.6.4/Modules/spwdmodule.o/usr/bin/pwd/usr/bin/pwdx/usr/include/pwd.h/usr/lib/golang/src/syscall/pwd_plan9.go/usr/lib/modules/3.10.0-1062.9.1.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko.xz/usr/lib/modules/3.10.0-1160.11.1.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko.xz/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko.xz/usr/lib64/cracklib_dict.pwd/usr/lib64/python2.7/lib-dynload/spwdmodule.so/usr/lib64/python3.6/lib-dynload/spwd.cpython-36m-x86_64-linux-gnu.so/usr/local/python/lib/python3.6/lib-dynload/spwd.cpython-36m-x86_64-linux-gnu.so/usr/local/python/lib/python3.6/test/test_pwd.py/usr/local/python/lib/python3.6/test/test_spwd.py/usr/local/python/lib/python3.6/test/__pycache__/test_pwd.cpython-36.opt-1.pyc/usr/local/python/lib/python3.6/test/__pycache__/test_pwd.cpython-36.opt-2.pyc/usr/local/python/lib/python3.6/test/__pycache__/test_pwd.cpython-36.pyc/usr/local/python/lib/python3.6/test/__pycache__/test_spwd.cpython-36.opt-1.pyc/usr/local/python/lib/python3.6/test/__pycache__/test_spwd.cpython-36.opt-2.pyc/usr/local/python/lib/python3.6/test/__pycache__/test_spwd.cpython-36.pyc/usr/local/share/jcloud/jdog-monitor/deploy/kunlunmirror/9_de96bb875c9807de5e4a6e11fdea0b07/lib64/python3.6/lib-dynload/spwd.cpython-36m-x86_64-linux-gnu.so/usr/local/share/jcloud/jdog-monitor/deploy/kunlunmirror/9_de96bb875c9807de5e4a6e11fdea0b07/lib64/python3.6/test/test_pwd.py/usr/local/share/jcloud/jdog-monitor/deploy/kunlunmirror/9_de96bb875c9807de5e4a6e11fdea0b07/lib64/python3.6/test/test_spwd.py/usr/sbin/unix_chkpwd/usr/share/cracklib/cracklib-small.pwd/usr/share/cracklib/pw_dict.pwd/usr/share/fish/functions/__fish_pwd.fish/usr/share/fish/functions/prompt_pwd.fish/usr/share/fish/man/man1/prompt_pwd.1/usr/share/fish/man/man1/pwd.1/usr/share/man/man1/pwd.1.gz/usr/share/man/man1/pwdx.1.gz/usr/share/man/man8/unix_chkpwd.8.gz/usr/share/man/mann/pwd.n.gz
搜索etc目录下所有以sh开头的文件
> locate /etc/sh/etc/shadow/etc/shadow-/etc/shells
输出locate所使用的数据库文件的相关信息,包括该数据库记录的文件/目录数量等
> locate -SDatabase /var/lib/mlocate/mlocate.db:        289,742 directories        516,673 files        60,174,514 bytes in file names        52,034,824 bytes used to store database
updatedb的配置文件/etc/updatedb.conf
> 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 fuse.sshfs fusectl gfs gfs2 gpfs 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 fuse.glusterfs ceph fuse.ceph"PRUNENAMES = ".git .hg .svn"PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph"
PRUNE_BIND_MOUNTS = “yes” #开启搜索限制PRUNEFS = #搜索时不搜索的文件系统PRUNENAMES = #搜索时不搜索的文件类型PRUNEPATHS = 搜索时不搜索的路径

标签: #linux 查找命令 locate