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.,-ifor case-insensitive,-efor 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 Type | Description | Example | Use Case | |
|---|---|---|---|---|
| -- | - | - | - | |
| Plain string | Matches any part of the filename or path | locate config | Find all files containing “config” | |
Wildcard (*) | Matches zero or more characters | locate "*.php" | List all PHP files | |
| Suffix search | Match file extension | locate ".sql" | Find SQL backups | |
| Prefix search | Match directory name prefix | locate "/var/log" | Find all log files | |
Basename match (-b) | Matches filename only, not full path | locate -b "\index.php" | Find exact filename regardless of directory | |
Regex (-r) | Advanced regular expression matching | `locate -r ".*.(jpg | png)$"` | Match multiple image extensions |
Case-insensitive (-i) | Ignore case in pattern | locate -i STYLE.CSS | Match style.css, STYLE.css, etc. | |
| Exact match | Exact string match when quoted | locate "wp-config.php" | Find specific configuration file |
Important Options
| Option | Description |
|---|---|
| -- | |
-i | Case-insensitive search |
-c | Count results only |
-n NUM | Limit number of results |
-r REGEX | Use regex pattern |
-b | Match basename only |
-e | Show only existing files |
-q | Quiet mode |
-0 | NUL-separated output |
--help | Show help |
--version | Show 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
Case-insensitive CSS search
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
| Limitation | Explanation |
|---|---|
| - | |
| Search file contents | Use rg or grep instead |
| Execute commands directly | Use find, xargs, or parallel |
| Real-time search | Results depend on database freshness |
| Filter by size/date/owner | No metadata filtering |
| Fully respect permissions dynamically | Searches based on database index |
Comparison with Other Tools
| Tool | Primary Purpose |
|---|---|
| - | - |
locate | Fast filename search (database-based) |
fd | Modern, safe filename search |
rg | Search inside file contents |
find | Advanced search + execution |
parallel | Execute tasks concurrently |
locate complements but does not replace these tools.
Best Practices
- Always run
sudo updatedbbefore important searches. - Use
-eto avoid stale entries. - Use
-nto prevent overwhelming output. - Combine with
xargs -0when automating. - Avoid destructive pipelines directly from
locatewithout verification. - Do not rely on
locatefor security-sensitive real-time investigations.
Troubleshooting
| Problem | Cause | Solution |
|---|---|---|
| -- | -- | |
| No results | Database outdated | Run sudo updatedb |
| Too many results | Pattern too broad | Use -n or regex |
| Deleted file appears | Stale database | Refresh database |
| Missing directories | Not indexed in config | Check /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
- Why is
locatefaster thanfind? - What command refreshes the locate database?
- Which option counts results instead of printing them?
- How do you search using regex?
- Why should
-ebe used in automation?