`
tianshibaijia
  • 浏览: 1129179 次
文章分类
社区版块
存档分类
最新评论

linux locate 学习笔记

 
阅读更多
locate 命令

part 1: 使用locate manual
$ man locate
locate(1) locate(1)
NAME
locate - find files by name
SYNOPSIS
locate [OPTION]... PATTERN...
part 2: locate 的搜索机制

whereis 严格来讲是从你环境变量的路径(path等主要路径)中查找目标。速度快,范围有限。只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

举例:

$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz
$ whereis -b grep
grep: /bin/grep
$ whereis -m grep
grep: /usr/share/man/man1/grep.1.gz
$ whereis -s grep
grep:


locate则是从 updatedb命令生成的数据库中查找目标全盘文件检索 ,需要要定时运行updatedb更新数据库 ,以保证内容的实时性

举例:

$ locate xyz.py # 查找一个不出在的文件 xyz.py, 找不到结果
$ touch xyz.py # 手动建立xyz.py, 并
$ locate xyz.py # 使用 locate 查找,仍然查不到
$ sudo updatedb # 更新数据库
$ locate xyz.py # 再次locate 查找, okay
/home/jia/xyz.py


find 是“最强大,最复杂,最灵活” 的查找命令,可以用它找到任何你想找的文件


which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
$ which find
/usr/bin/find
$ which cp
/bin/cp
$ which grep
/bin/grep




type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。
$ type cd

cd is a shell builtin
$ type python
python is /usr/bin/python
$ type -p python
/usr/bin/python
$ which python
/usr/bin/python


part 3: OPTIONS

param 1:
-b, --basename
Match only the base name against the specified patterns.
This is the opposite of --wholename.
# 查询文件系统中文件名含有 pattern 的文件
$ locate -b 'ue' |less
param 2:
-c, --count
Instead of writing file names on standard output, write
the number of matching entries only.
# 查询文件系统中文件名含有 pattern 的文件的个数
$ locate -c 'ue'

param 3:
-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 stan鈥?
dard input only once.
# 指定所以来的数据库的路径(一般使用缺省的数据库)


param 4:

-e, --existing
Print only entries that refer to files existing at the
time locate is run.
# 显示执行locate 命令时匹配的文件列表。

param 5:
-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 speci鈥?
fied using --nofollow.
# 统计是将链接文件一并统计在内

param 6:
-i, --ignore-case
Ignore case distinctions when matching patterns.
# 进行文件名匹配时,不区分大小写

param 7:
-l, --limit, -n LIMIT
Exit successfully after finding LIMIT entries. If the
--count option is specified, the resulting count is also
limited to LIMIT.
# 进行文件名匹配时,只显示指定数目的匹配结果


param 8:

-w, --wholename

Match only the whole path name against the specified pat鈥?
terns.
This is the default behavior. The opposite can be speci鈥?
fied using --basename.
# 进行文件名匹配时,进行全名匹配。 即 locate -w 'ue' , 只会查找出文件名为 ue 的文件, 文件名中含有 ue 的将不会被统计出。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics