Locate a File in FreeBSD Safely and Securely – How to

filesfreebsdlocate

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?

Best Answer

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.