Search for Files by Name with locate in Linux
Learn how locate searches a filename database, refresh it with updatedb, troubleshoot stale results, and choose locate or find for Linux file searches.
The locate command searches for filesystem paths and filenames that contain specified text. It is designed for fast filename searches, not for detailed filtering by permissions, ownership, size, or timestamps.
This lesson assumes familiarity with the Linux terminal, shell commands, files, directories, and basic sudo use. For background on paths and directories, see File Structure in Linux.
What locate Does
The basic command structure is:
locate SEARCH_TERMlocate finds indexed paths whose names or path text match SEARCH_TERM. It does not normally walk through every directory during each search. Instead, it reads a prebuilt locate database, which is an index containing filesystem paths and filenames.
Because the command looks up text in an index rather than performing a live filesystem search, it usually responds very quickly. This makes it useful when you remember part of a filename and want possible matching paths.
Basic Filename Searches
Search for a specific filename
To search for paths containing notes.txt, run:
locate notes.txtThe output may contain one or many paths. For example:
/home/alex/notes.txt
/home/alex/Documents/notes.txt
/home/alex/old-notes.txtThe command searches for the supplied text in indexed paths. Therefore, a result does not necessarily mean that the final component of the path is exactly the search term. A path such as /home/alex/old-notes.txt can also match because it contains notes.txt.
Search with a name fragment
You can search for part of a name, such as:
locate logThis can return paths containing log, including names such as system.log, catalog, or directories with log in their path. Broad terms can produce many results, so use a more specific filename or path fragment when possible.
How the locate Database Works
A system periodically builds or refreshes the locate database by examining filesystem paths. The schedule depends on the system configuration; it may run daily, weekly, or on another maintenance schedule.
When you run locate, the command consults the saved index. It does not perform a live filesystem search, meaning it does not inspect the current state of every directory at command execution time.
This design explains both of locate's main characteristics:
- Fast results: searching an index is generally faster than traversing the filesystem.
- Possibly stale results: the index can differ from what currently exists on disk.
Why Recent Files May Be Missing
Suppose you create a file and immediately search for it:
touch recent-file.txt
locate recent-file.txtThe file exists on the live filesystem, but the command may print no output. The database might have been created before recent-file.txt existed, so the new path is not indexed yet.
The same issue can occur when a file has recently been moved or renamed. The old location may remain in the database while the new location is absent until the next database update. Conversely, a deleted file can continue to appear in locate output while its old entry remains indexed.
| Situation | Likely reason | Recommended action |
|---|---|---|
| File was created recently | The database predates the file. | Refresh the database with updatedb, then search again. |
| File was moved or renamed recently | The index still has the old path and may not have the new path. | Refresh the database or use find for a current search. |
| File was deleted but still appears | An older path entry remains in the index. | Refresh the database or verify the live filesystem with find. |
| Database update has not run yet | The scheduled maintenance task has not refreshed the index. | Run updatedb manually if authorized. |
Refreshing the Database with updatedb
updatedb creates or refreshes the database used by locate. Run it before repeating a search when the index is out of date:
sudo updatedb
locate recent-file.txtAfter updatedb completes, the newly created file should be included in the index, and the second command can return its path.
Administrative permissions may be required because the update can inspect many parts of the system and create a system-wide database. On systems where the current user is permitted to update the database directly, the command may work without sudo:
updatedbIf the command is rejected because of permissions, use sudo updatedb when your account is authorized, or follow your system administrator's policy.
locate Compared with find
find performs a live filesystem search. It traverses directories and evaluates the current filesystem state while the command runs. This can take longer, especially when starting at the root directory, but its results reflect changes that have already occurred.
locate notes.txt
find / -name 'notes.txt' 2>/dev/nullThe first command queries the locate database. The second command starts at /, searches the filesystem directly, and hides many permission-denied messages by redirecting standard error to /dev/null.
| Characteristic | locate | find |
|---|---|---|
| Search method | Queries a prebuilt filename and path database. | Traverses the filesystem and evaluates entries live. |
| Speed | Generally very fast for name and path lookups. | Can be slower because it examines directories during the search. |
| Data freshness | Can be stale until the database is updated. | Reflects the filesystem state at search time. |
| Primary search criteria | Names and path text. | Names, paths, file types, times, sizes, permissions, ownership, and more. |
| Filter by permissions, owner, and size | Not its intended purpose. | Yes, using appropriate find predicates. |
| Typical use case | Quickly find possible paths from a remembered filename or fragment. | Perform a current search or apply detailed file-property conditions. |
Use locate when speed and filename matching are the main requirements. Use find when accuracy against the current filesystem or attribute filtering matters. For example, file ownership concepts are covered in Manage File Ownership.
Troubleshooting locate
No result for a file that was just created
Cause: The database was built before the file existed.
Fix:
sudo updatedb
locate recent-file.txtA result points to a file that no longer exists
Cause: The database contains an older path entry.
Fix: Refresh the index with updatedb, or use find to check the live filesystem.
Too many results appear
Cause: The search term is a common fragment that occurs in many indexed paths.
Fix: Use a more specific term, such as a complete filename or a distinctive directory name:
locate project-notes.txtupdatedb cannot be run
Cause: The system-wide database requires elevated privileges.
Fix: Run sudo updatedb if you are authorized. If your account cannot use sudo, ask the administrator or wait for the configured maintenance update.
The search depends on permissions, ownership, or size
Cause: locate is intended for indexed filename and path matching, not detailed live attributes.
Fix: Use find with the appropriate filters. A live search is also preferable when you must be certain that results reflect the current filesystem.
Practical Decision Guide
- Need a quick search based on a filename or path fragment? Use
locate SEARCH_TERM. - Did the file change recently or must the result be current? Use
find, or refresh the index first withupdatedb. - Need to filter by permissions, owner, size, timestamps, or file type? Use
find. - Did a broad search return too many paths? Replace the common fragment with a more specific filename.
Key Points
locatesearches indexed filenames and paths.- Its database-based design usually makes it faster than a live traversal.
- New, moved, renamed, or deleted files may not be represented immediately.
updatedbrefreshes the database;sudomay be required.findsearches the live filesystem and supports detailed attribute filters.