3

In FreeBSD 12, on a freshly-created virtual machine (DigitalOcean), I tried to use the locate command.

$ locate java

I received an error.

locate: database too small: /var/db/locate.database

Run /usr/libexec/locate.updatedb

So I ran locate.updatedb.

$ /usr/libexec/locate.updatedb

Got a message, complaining about permissions.

/usr/libexec/locate.updatedb: cannot create /var/db/locate.database: Permission denied

Okay. Run as sudo.

$ sudo /usr/libexec/locate.updatedb

I got a security warning.

WARNING

Executing updatedb as root. This WILL reveal all filenames

on your machine to all login users, which is a security risk.

Unix is so much fun.

➥ What is the proper secure way to find a file or directory by name on your FreeBSD system?

2
4

locate is an easy way to search for a file quickly since it has it's own database. However, I always just use find(1). The results are returned to the user who ran it, and the user who ran it can only find files they have the appropriate file system permissions to.

find searches recursively, so you can specify / as the search path if you want to search every filesystem.

Finding all files and directories named foo:

find / -name "foo"

Finding only files named foo:

find / -type f -name "foo"

Finding only directories named foo:

find / -type d -name "foo"

There are a lot of useful options. Check out the man page.

1
  • 2
    Also, find doesn't require a database, so its answers will always be up to date, modulo any changes that may occur while find is running. – Jim L. Jul 23 '19 at 22:34
2

run periodic

As you have learned, the locate command uses a database storing an indexed collection of your file and folder names. Because your installation of FreeBSD is brand-new, that database has not yet been built.

When will the database be built? When FreeBSD performs its regular housekeeping chores. There are batches of such chores to be done automatically on a daily, weekly, and monthly basis. See the periodic command.

You may want to perform those housekeeping chores immediately, after creating your virtual machine. Run:

periodic daily
periodic weekly
periodic monthly

Or run all three in one line.

periodic daily weekly monthly

In my experience, these take about a minute each.

➥ In particular, the weekly one seems to create/update the locate database. When you cannot find something you know exists, do run periodic weekly.

If curious, you could check the existence and size of your locate database before and after running periodic. Likely found here: /var/db/locate.database

You should now be able to use the locate command successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.