Skip to content

Instantly share code, notes, and snippets.

@antonkratz
Last active December 6, 2018 20:30
Show Gist options
  • Save antonkratz/f898bd0b639d389402a2920505ae948c to your computer and use it in GitHub Desktop.
Save antonkratz/f898bd0b639d389402a2920505ae948c to your computer and use it in GitHub Desktop.
mlocate locate bash updatedb

Building a separate database enables you to locate files on sshfs-mounted filesystems

The find command is very slow. locate is much faster, but relies on a database, which is typically build nightly. Often I have mounted remote filesystems using sshfs. By default, the database is not build for sshfs-mounted filesystems.

Build an mlocate database for the sshfs-mounted dir /home/kratz/mpoints/cellar (see below for cron version):

updatedb -l 0 -o ~/.cellar.db -U /home/kratz/mpoints/cellar

Example

Try to find the file size_order.png - it cannot be found

kratz@tower3620:~ $ locate size_order.png

Try to find it in the custom database - it will be found

kratz@tower3620:~ $ locate -d ~/.cellar.db size_order.png
/home/kratz/mpoints/cellar/git/project/assets/size_order.png

Use -i to ignore the case during database lookup

See section header. If you are not sure about the case of the file you are searching for.

kratz@tower3620:~ $ locate -d ~/.cellar.db Size_order.png
kratz@tower3620:~ $ locate -i -d ~/.cellar.db Size_order.png
/home/kratz/mpoints/cellar/git/project/assets/size_order.png

Cron please do this every night at 1 am

Sometimes you want to update the database manually, especially after there have been massive changes, but it is also good to have nightly builds. See your user (!) crontab file with crontab -l and edit it with crontab -e.

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 1 * * * updatedb -l 0 -o ~/.cellar.db -U /home/kratz/mpoints/cellar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment