Data Management

Tech Tip: Find files with slocate

Here's how to find files with slocate.

When searching for a file, the first command most users try is the which command. However, this works only for files included in the user's PATH. If a file isn't in the user's PATH, the which command will never find it.

The next option might be the find command itself, but this can be overkill. Find takes time because it looks in each directory and at each file to match the requested criteria. This is great if you're looking for files with certain permissions or ownership, but it can be time-consuming when you're searching for a particular file by name.

Another option is the slocate command, which comes standard with most Linux distributions. This command searches for files by looking them up in a database, which a nightly cron job typically updates.

After installing slocate, you must create the database that it searches. Depending on the size of your file system, the time required to construct the database will vary.

To initialize the database, use the following:

# slocate -c -u

After creating the database, create a cron job to run slocate (as shown above) every night to update the database.

To find a file, execute the following:

$ slocate myfile

This command will find the "myfile" file, no matter where on the system it resides. If similar files exist, such as "myfiles" or "test-myfile," the command will find those files as well.

There are also a number of options you can pass to this command. You can use slocate to search for files by passing it a regular expression, you can perform case-insensitive searches, and you can limit the number of results returned.

Editor's Picks

Free Newsletters, In your Inbox