每天一个linux命令(18):locate 命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。locate命令其实是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。通过man page可以看到updatedb是一个管理工具,它用来更新索引数据库mlocate.db,所以普通用户无法使用updatedb命令。报错如下:
updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'
1.命令格式:
Locate [选择参数] [样式]
2.命令功能:
locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的,locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快,但较差劲的是locate所找到的档案若是最近才建立或刚更名的,可能会找不到,在内定值中,updatedb每天会跑一次,可以由修改crontab来更新设定值。(etc/crontab)
locate指定用在搜寻符合条件的档案,它会去储存档案与目录名称的数据库内,寻找合乎范本样式条件的档案或目录录,可以使用特殊字元(如”*” 或”?”等)来指定范本样式,如指定范本为kcpa*ner, locate会找出所有起始字串为kcpa且结尾为ner的档案或目录,如名称为kcpartner若目录录名称为kcpa_ner则会列出该目录下包括 子目录在内的所有档案。
locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在执行loacte时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接下达updatedb强迫系统立即修改索引数据库。
Manual
NAME
locate - find files by name
SYNOPSIS
locate [OPTION]... PATTERN...
DESCRIPTION
locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line. If --regex is not specified, PATTERNs can contain globbing characters(通配符字符). If any PATTERN contains no globbing characters, locate behaves as if the pattern were *PATTERN*.(模糊查询,哈哈)
By default, locate does not check whether files found in database still exist. locate can never report files created after the most recent update of the relevant database.
EXIT STATUS
locate exits with status 0 if any match was found or if locate was invoked with one of the --limit 0, --help, --statistics or --version options. If no
match was found or a fatal error was encountered, locate exits with status 1.
Errors encountered while reading a database are not fatal(致命的), search continues in other specified databases, if any.
OPTIONS
-b, --basename
Match only the base name against the specified patterns. This is the opposite of --wholename.
-c, --count
Instead of writing file names on standard output, write the number of matching entries only.
-d, --database DBPATH
Replace the default database with DBPATH. DBPATH is a :-separated list of database file names. If more than one --database option is specified,
the resulting path is a concatenation of the separate paths.
An empty database file name is replaced by the default database. A database file name - refers to the standard input. Note that a database can be
read from the standard input only once.
-e, --existing
Print only entries that refer to files existing at the time locate is run.(仅仅输出当locate运行时,存在于磁盘的文件)
-L, --follow
When checking whether files exist (if the --existing option is specified), follow trailing(拖尾?) symbolic links. This causes broken symbolic links to be
omitted from the output.
This is the default behavior. The opposite can be specified using --nofollow.
-h, --help
Write a summary of the available options to standard output and exit successfully.
-i, --ignore-case
Ignore case distinctions(区别) when matching patterns.
-l, --limit, -n LIMIT
Exit successfully after finding LIMIT entries. If the --count option is specified, the resulting count is also limited to LIMIT.
-m, --mmap
Ignored, for compatibility with BSD and GNU locate.
-P, --nofollow, -H
When checking whether files exist (if the --existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links
to be reported like other files.
This is the opposite of --follow.
-0, --null
Separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line. This option is designed for interop‐
erability with the --null option of GNU xargs(1).
-S, --statistics
Write statistics about each read database to standard output instead of searching for files and exit successfully.
-q, --quiet
Write no messages about errors encountered while reading and processing databases.
-r, --regexp REGEXP
Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.
--regex
Interpret all PATTERNs as extended regexps.
-s, --stdio
Ignored, for compatibility with BSD and GNU locate.
-V, --version
Write information about the version and licence of locate on standard output and exit successfully.
-w, --wholename
Match only the whole path name against the specified patterns.
This is the default behavior. The opposite can be specified using --basename.
EXAMPLES
To search for a file named exactly NAME (not *NAME*), use
locate -b '\NAME'
Because \ is a globbing character(通配符字符), this disables the implicit replacement of NAME by *NAME*.
FILES
/var/lib/mlocate/mlocate.db
The database searched by default.
ENVIRONMENT
LOCATE_PATH
Path to additional databases, added after the default database or the databases specified using the --database option.
NOTES
The order in which the requested databases are processed is unspecified, which allows locate to reorder the database path for security reasons.
locate attempts to be compatible to slocate (without the options used for creating databases) and GNU locate, in that order. This is the reason for the
impractical(不切实际的) default --follow option and for the confusing set of --regex and --regexp options.
The short spelling of the -r option is incompatible to GNU locate, where it corresponds to(相当于…) the --regex option. Use the long option names to avoid confusion.
The LOCATE_PATH environment variable replaces the default database in BSD and GNU locate, but it is added to other databases in this implementation and
slocate.
AUTHOR
Miloslav Trmac <mitr@redhat.com>
SEE ALSO
updatedb(8)
3.命令参数:驴唇不对马嘴,下面的参数都是updatedb命令的
-e 将排除在寻找的范围之外。
-1 如果是1.则启动安全模式。在安全模式下,使用者不会看到权限无法看到的档案。这会使速度减慢,因为 locate 必须至实际的档案系统中取得档案的权限资料。
-f 将特定的档案系统排除在外,例如我们没有道理把 proc 档案系统中的档案放在资料库中。
-q 安静模式,不会显示任何错误讯息。
-n 至多显示 n 个输出。
-r 使用基本正则表达式做寻找的条件。
-o 指定资料库存的名称。
-d 指定资料库的路径
-h 显示辅助讯息
-V 显示程式的版本讯息
4.使用实例:
实例1:查找所有文件名包含了pwd字符串的文件
命令:
locate pwd
输出:
peida-VirtualBox ~ # locate pwd
/bin/pwd
/etc/.pwd.lock
/sbin/unix_chkpwd
/usr/bin/pwdx
/usr/include/pwd.h
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc
/usr/lib/syslinux/pwd.c32
/usr/share/help/C/empathy/irc-join-pwd.page
/usr/share/help/ca/empathy/irc-join-pwd.page
/usr/share/help/cs/empathy/irc-join-pwd.page
/usr/share/help/de/empathy/irc-join-pwd.page
/usr/share/help/el/empathy/irc-join-pwd.page
实例2: 搜索etc目录下所有以sh开头的文件 搜索以sh开头,上级目录是etc的文件。
命令:
locate /etc/sh
输出:
peida-VirtualBox ~ # locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
peida-VirtualBox ~ #
实例3:搜索etc目录下,所有以m开头的文件 搜索以m开头,上级目录是etc的文件。
命令:
locate /etc/m
输出:
peida-VirtualBox ~ # locate /etc/m
/etc/magic
/etc/magic.mime
/etc/mailcap
/etc/mailcap.order
/etc/manpath.config
/etc/mate-settings-daemon
/home/kenm6/liujin/1018-FWVGA-td-licheng-s12_foreign/sdk/sdkmanager/libs/sdkuilib/etc/manifest.txt
/home/kenm6/liujin/1018-FWVGA-td-licheng-s12_foreign/sdk/traceview/etc/manifest.txt
/home/kenm6/liujin/1018-FWVGA-td-licheng-s12_foreign/sdk/uiautomatorviewer/etc/manifest.txt
/home/kenm6/liujin/1018-FWVGA-td-licheng-s12_foreign/system/core/rootdir/etc/mountd.conf

浙公网安备 33010602011771号