Updating Index DB for locate command

Unix Tutorial

I sometimes forget there is a great locate command available in most distros, which is possibly the quickest way of finding a file or directory by their name. locate is super fast because it searches through index databases and not the filesystems directly. Here’s how you can update index DB for locate.

Why Do We Need Index Databases for Locate?

locate has one goal: find you files as quickly as possible. And while sometimes you need a real-time information, usually it’s enough if results of locate command are just recent, but not immediately fresh.

What this mean is that indexing of your filesystem can happen at any time, preferably daily, so that when you are looking for files with locate it has a fresh enough index of all the files in the available filesystems.

In other words, searching for files with locate command should NOT mean any of this:

  • indexing filesytems
  • updating indexes
  • searching on the filesystems directly

That’s why index databases for locate exist and that’s also why you do not have any delay with receiving results when you run locate.

Updating locate databases with updatedb

If your system doesn’t have a locate database, you would receive an error when using locate command:

greys@rhel8:~ $ locate sudoers
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

To fix this, simply regenerate the index database mlocate.db:

greys@rhel8:~ $ sudo updatedb

Should only take a few minutes, after that locate will start working:

greys@rhel8:~ $ locate sudoers
/etc/sudoers
/etc/sudoers.d
/usr/bin/cvtsudoers
/usr/libexec/sudo/sudoers.so
/usr/share/augeas/lenses/dist/sudoers.aug
/usr/share/doc/sudo/examples/sudoers
/usr/share/locale/ca/LC_MESSAGES/sudoers.mo
/usr/share/locale/cs/LC_MESSAGES/sudoers.mo
/usr/share/locale/da/LC_MESSAGES/sudoers.mo
/usr/share/locale/de/LC_MESSAGES/sudoers.mo

That’s it for today! Have fun!

See Also