Skip to main content

locate — Fast File Search Using Database

Introduction​

locate is a fast file search utility that retrieves results from a prebuilt database instead of scanning the filesystem in real time.

Unlike find, which walks the directory tree on each execution, locate queries a cached index (typically mlocate.db). This makes it extremely fast for filename-based searches.

However, because it depends on a database snapshot, results may be outdated unless the database is refreshed.

locate is best suited for:

  • Fast filename lookups
  • Discovering config files
  • Locating logs or backups
  • Quickly identifying suspicious filenames

It is not suitable for content searching or executing actions directly.

Installation and Setup​

Install:

sudo apt update
sudo apt install mlocate -y

Update the search database:

sudo updatedb

Verify installation:

which locate

Expected output:

/usr/bin/locate

Check version:

locate --version

Syntax & Expression Rules​

The command follows a logical structure that reads almost like a sentence:

locate [OPTION] PATTERN
  • [OPTION]: Flags to modify search behavior (e.g., -i for case-insensitive, -e for existing).
  • [PATTERN]: The filename or expression to search for.

Pattern Types​

locate supports simple string matching and limited regex.

You’re absolutely right — I unintentionally simplified the Pattern Types section and removed the Exact match row.

Below is the corrected Pattern Types section, restored to match your original structure and completeness. You can directly replace that section in your Docusaurus file.

Pattern Types in locate​

PATTERN defines what you want to search. It can be a plain string, wildcard, or regular expression.

Pattern TypeDescriptionExampleUse Case
-----
Plain stringMatches any part of the filename or pathlocate configFind all files containing “config”
Wildcard (*)Matches zero or more characterslocate "*.php"List all PHP files
Suffix searchMatch file extensionlocate ".sql"Find SQL backups
Prefix searchMatch directory name prefixlocate "/var/log"Find all log files
Basename match (-b)Matches filename only, not full pathlocate -b "\index.php"Find exact filename regardless of directory
Regex (-r)Advanced regular expression matching`locate -r ".*.(jpgpng)$"`Match multiple image extensions
Case-insensitive (-i)Ignore case in patternlocate -i STYLE.CSSMatch style.css, STYLE.css, etc.
Exact matchExact string match when quotedlocate "wp-config.php"Find specific configuration file

Important Options​

OptionDescription
--
-iCase-insensitive search
-cCount results only
-n NUMLimit number of results
-r REGEXUse regex pattern
-bMatch basename only
-eShow only existing files
-qQuiet mode
-0NUL-separated output
--helpShow help
--versionShow version

WordPress / VPS Examples​

Locate WordPress config​

locate wp-config.php

Locate themes directory​

locate wp-content/themes

Locate plugins directory​

locate wp-content/plugins
locate -i STYLE.CSS

Count number of WordPress installs​

locate -c wp-config.php

Limit output preview​

locate -n 3 error.log

Find SQL backups​

locate -r "\.sql$"

Match basename only​

locate -b "\index.php"

Show only existing files​

locate -e debug.log

Locate server error logs​

locate error.log

Locate uploads folder by year​

locate uploads/2025

Locate hidden files​

locate .htaccess

Locate database configuration​

locate my.cnf

Use NUL-safe output for scripting​

locate -0 "*.sql" | xargs -0 ls -lh

Capabilities and Limitations​

What locate Can Do​

  • Extremely fast filename search
  • Pattern matching with wildcards or regex
  • Case-insensitive search
  • Count or limit results
  • Safe NUL-separated output

What locate Cannot Do​

LimitationExplanation
-
Search file contentsUse rg or grep instead
Execute commands directlyUse find, xargs, or parallel
Real-time searchResults depend on database freshness
Filter by size/date/ownerNo metadata filtering
Fully respect permissions dynamicallySearches based on database index

Comparison with Other Tools​

ToolPrimary Purpose
--
locateFast filename search (database-based)
fdModern, safe filename search
rgSearch inside file contents
findAdvanced search + execution
parallelExecute tasks concurrently

locate complements but does not replace these tools.

Best Practices​

  • Always run sudo updatedb before important searches.
  • Use -e to avoid stale entries.
  • Use -n to prevent overwhelming output.
  • Combine with xargs -0 when automating.
  • Avoid destructive pipelines directly from locate without verification.
  • Do not rely on locate for security-sensitive real-time investigations.

Troubleshooting​

ProblemCauseSolution
----
No resultsDatabase outdatedRun sudo updatedb
Too many resultsPattern too broadUse -n or regex
Deleted file appearsStale databaseRefresh database
Missing directoriesNot indexed in configCheck /etc/updatedb.conf

Cheat Sheet​

locate wp-config.php
locate -i style.css
locate -c .php
locate -n 5 error.log
locate -r "\.sql$"
locate -b "\index.php"
locate -e debug.log
locate -0 "*.sql" | xargs -0 ls -lh
sudo updatedb

Mini Quiz​

  1. Why is locate faster than find?
  2. What command refreshes the locate database?
  3. Which option counts results instead of printing them?
  4. How do you search using regex?
  5. Why should -e be used in automation?