Mac上使用locate
系统级locate
以下用于搜整个系统的文件
首次使用locate
初次使用locate前, 需要执行以下命令, 开启一个开机自启进程, 用以创建 locate 的数据库
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
一段时间后, 命令执行完, locate数据库初次创建好, 此后可以 locate (部分)文件名 查找文件的路径
此自启进程会每周重建一次数据库, 如果你需要修改更新的频率, 详见下文 数据库自动更新
手动 建立/更新数据库
LInux下: 运行命令 sudo updatedb
Mac下: 运行命令 sudo /usr/libexec/locate.updatedb,
- 必需sudo执行, 以便遍历整个系统的文件
- 将locate数据库生成于
/private/var/db/locate.database/var/db/locate.database文件 - 此二文件互为硬链接, 有相同文件id
报错 mktemp: too few X's in template ‘updatedb’
当执行
sudo /usr/libexec/locate.updatedb
会报错
mktemp: too few X's in template ‘updatedb’
chown: missing operand after ‘nobody’
Try 'chown --help' for more information.
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
^Crmdir: /tmp/locatefO9MXysbH8/mklocate1UecfP0H1n: No such file or directory
rmdir: /tmp/locatefO9MXysbH8: No such file or directory
install: missing destination file operand after '/var/db/locate.database'
Try 'install --help' for more information.
rm: missing operand
Try 'rm --help' for more information.
原因: 装了coreutils, 致使 mktemp 命令被替换成 coreutils 的 mktemp. 而updatedb只能使用 OSX 自带的 mktemp, 故而报错.
解决方法: 将 /usr/libexec/locate.updatedb中所有 mktemp 换成 /usr/bin/mktemp.
数据库自动更新
/System/Library/LaunchDaemons/com.apple.locate.plist 文件是locate数据库自动更新的程序. 上文执行 sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist 后, 每次开机均会以 root 加载此程序, 它会定时更新locate数据库.
/System/Library/LaunchDaemons/com.apple.locate.plist 文件中以下三个字段设置了如何更新 locate 的数据库.
-
执行命令
<key>ProgramArguments</key> <array> <string>/usr/libexec/locate.updatedb</string> </array>即执行
sudo /usr/libexec/locate.updatedb -
数据库路径
<key>PathState</key> <dict> <key>/var/db/locate.database</key> <false/> </dict>数据库文件
/var/db/locate.database -
更新频率
默认设置如下, 意为每周六3:15自动更新
<key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>3</integer> <key>Minute</key> <integer>15</integer> <key>Weekday</key> <integer>6</integer> </dict>如需改为每天3:15, 则将
6改为*, 或直接注释掉<key>Weekday</key> <integer>6</integer>如需设置每n秒自动更新一次, 则注释掉整个
StartCalendarInterval字段, 在其下插入如下代码, 此为每3小时(即108000秒)更新一次<key StartInterval</key> <integer>108000</integer>
个人级locate
以下用于搜个人目录中的文件
报错 mktemp: too few X's in template ‘updatedb’
依照上文 系统级locate / 报错 mktemp: too few X's in template ‘updatedb’ 进行设置
设置自动更新
设置当前用户的定时执行程序
crontab -e
- 注意不要加
sudo. 加sudo则是为root设置定时执行程序.
弹出vim, 输入
# cron配置最开始设定
SHELL=/bin/zsh
# ...
# 只搜索我的私人文件,数据库文件放自己的目录,一个小时运行一次
5 * * * * SEARCHPATHS=$HOME FCODES=$HOME/.locate.db /usr/libexec/locate.updatedb
保存, 退出.
此设置的含义为:
-
5 * * * *: 每小时的05分时执行时间的格式, 参见 修改crontab执行时间
* * * * * command 分 时 日 月 周 命令 0~59 0-23 1~31 1-12 0-7(0=7=周日)以设置”分”为例:
*: 每分钟执行5: 05(分)之时执行*/5: 每隔5(分)执行15,45,50: 15分,45分,50分之时执行
整体例如
0 0,12 * * *: 每天0:00和12:00执行 -
执行
/usr/libexec/locate.updatedb -
SEARCHPATHS=$HOME: 遍历$HOME以生成locate数据库 -
FCODES=$HOME/.locate.db: 数据库生成到$HOME/.locate.db文件
设置alias
在 ~/.zshrc 或 ~/.bashrc 加入
alias mylocate='command locate -i -d ~/.locate.db'
-i: 忽略大小写-d ~/.locate.db: 指定数据库文件
之后, 输入mylocate 则只搜 $HOME 里的文件.