locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。
其方法是先建立一个包括系统内所有档案名及路径的数据库,之后查询时就 只查询这个数据库 ,而不必实际深入档案系统之中了。
在一般的 distribution 中,数据库的建立都被放在 crontab 中自动执行;
命令格式 - locate [选择参数] [样式]
命令功能 -
命令参数 -
命令实例 -
实例一:系统第一次使用时,创建该数据库
命令
xxxxxxxxxx
21// 任何与数据库有关的操作,比如
2locate pwd
3// Mac_Terminal 下
4sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
输出
xxxxxxxxxx
131Qs-MacBook-Pro:dir1 qiu$ locate pwd
2
3WARNING: The locate database (/var/db/locate.database) does not exist.
4To create the database, run the following command:
5
6 sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
7
8Please be aware that the database can take some time to generate; once
9the database has been created, this message will no longer appear.
10
11Qs-MacBook-Pro:dir1 qiu$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
12Password:
13Qs-MacBook-Pro:dir1 qiu$
实例二:查找和 pwd 相关的所有文件
命令
xxxxxxxxxx
11locate pwd
输出
xxxxxxxxxx
331Qs-MacBook-Pro:dir1 qiu$ locate pwd
2/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_pwd.py
3/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_pwd.pyc
4/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_pwd.pyo
5/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_spwd.py
6/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_spwd.pyc
7/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/test/test_spwd.pyo
8/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/Dir/pwd-c.ri
9/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/FileUtils/pwd-c.ri
10/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/FileUtils/pwd-i.ri
11/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/Net/FTP/pwd-i.ri
12/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/Pathname/pwd-c.ri
13/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/share/ri/2.3.0/system/Shell/pwd-i.ri
14/System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/Documentation/Reference/Tcl/TclCmd/pwd.htm
15/System/Library/PrivateFrameworks/JavaScriptAppleEvents.framework/Versions/A/Resources/BridgeSupportCache/pwd.plist
16/System/Library/Tcl/8.5/tclx8.4/help/tcl/status/pwd
17/Users/qiu/mine/project/JianEssay/md/linux/pwd.html
18/Users/qiu/mine/project/LearnLinux/linux/pwd.md
19/Users/qiu/mine/project/vueDemo/my-project/node_modules/_shelljs@0.7.8@shelljs/src/pwd.js
20/Users/qiu/mine/study/mdTemp/pwd.html
21/bin/pwd
22/usr/sbin/pwd_mkdb
23/usr/share/cracklib/pw_dict.pwd
24/usr/share/man/man1/pwd.1
25/usr/share/man/man8/pwd_mkdb.8
26/usr/share/man/mann/pwd.ntcl
27/usr/share/zsh/5.3/functions/_external_pwds
28/usr/share/zsh/5.3/functions/chpwd_recent_add
29/usr/share/zsh/5.3/functions/chpwd_recent_dirs
30/usr/share/zsh/5.3/functions/chpwd_recent_filehandler
31/usr/share/zsh/5.3/functions/zftp_chpwd
32/usr/share/zsh/5.3/help/pwd
33Qs-MacBook-Pro:dir1 qiu$
实例三:搜索 etc 目录下所有以 m 开头的文件
命令
xxxxxxxxxx
1locate /etc/m
输出
x
101Qs-MacBook-Pro:dir1 qiu$ locate /etc/m
2/private/etc/mach_init.d
3/private/etc/mach_init_per_login_session.d
4/private/etc/mach_init_per_user.d
5/private/etc/mail.rc
6/private/etc/man.conf
7/private/etc/manpaths
8/private/etc/manpaths.d
9/private/etc/master.passwd
10/usr/share/emacs/22.1/etc/ms-kermit