🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

locate Command

Beginner File Viewing & Searching man(1)

Find files by name using a pre-built database

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
locate [OPTION]... PATTERN

What Does locate Do?

The locate command finds files by name using a pre-built database, making it orders of magnitude faster than find for simple name searches. It searches a database of file paths rather than traversing the filesystem.

The database is typically updated daily by a cron job running updatedb, or can be refreshed manually. Because it uses a database, locate cannot find files created since the last database update.

locate supports pattern matching with wildcards and regular expressions. It is ideal for quickly finding where a file or program is installed, locating configuration files, and general file discovery tasks where real-time accuracy is not critical.

Options & Flags

OptionDescriptionExample
-i Case-insensitive search locate -i readme
-c Print only the count of matches locate -c "*.conf"
-l Limit output to N results locate -l 10 nginx
-r Use POSIX regex instead of glob pattern locate -r "/etc/.*\.conf$"
-e Print only existing files (skip deleted) locate -e backup.sql
-b Match only the basename (filename without path) locate -b nginx.conf

Practical Examples

#1 Find a file quickly

Instantly finds all paths containing nginx.conf.
$ locate nginx.conf
Output: /etc/nginx/nginx.conf\n/etc/nginx/nginx.conf.bak

#2 Case-insensitive search

Finds README, readme, Readme, etc.
$ locate -i readme

#3 Count matching files

Shows how many PHP files exist in the database.
$ locate -c "*.php"
Output: 2847

#4 Update the database

Refreshes the locate database to include recently created files.
$ sudo updatedb

#5 Find only in basename

Matches only files named exactly nginx.conf (not paths containing it).
$ locate -b "\nginx.conf"
Output: /etc/nginx/nginx.conf

#6 Limit results

Shows only the first 5 matches.
$ locate -l 5 python

Tips & Best Practices

Database may be stale: locate uses a pre-built database. Newly created files will not appear until updatedb runs. Use find for real-time searches.
Fast file discovery: locate is 100-1000x faster than find for simple name searches. Use it as your first tool, then switch to find if you need freshness or complex criteria.
mlocate vs plocate: Modern Linux uses plocate (faster) or mlocate. Both provide the locate command with compatible interfaces.

Frequently Asked Questions

Why does locate not find my new file?
The locate database is updated periodically (usually daily). Run sudo updatedb to refresh it immediately.
What is the difference between find and locate?
locate searches a pre-built database (very fast but may be stale). find searches the live filesystem (slower but always current). locate is for quick lookups; find is for complex queries.
How do I update the locate database?
Run sudo updatedb. This scans the entire filesystem and rebuilds the database. It typically runs automatically via cron once per day.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →