VMware ESXi and vSphere Cluster Management
Search for Files by Name with locate in Linux
Learn how locate searches an indexed pathname database, use useful options, refresh it with updatedb, and choose find when live filesystem results are required.
locate is a Linux command for quickly searching file and directory pathnames. A pathname is a name together with its location in the directory hierarchy, such as /home/alex/Documents/report.txt.
Unlike a live filesystem search, locate normally queries a prebuilt locate database. This makes name-based searches fast, but the results can be out of date until the database is refreshed.
What locate Does
The basic command is:
locate search-pattern
locate searches indexed pathnames for the supplied pattern. It is designed primarily for finding names, not for inspecting file contents or evaluating detailed file metadata.
A database lookup is generally faster than walking directories one by one because locate reads an index prepared in advance. The trade-off is that the index may not immediately include files that were recently created, renamed, or deleted.
Basic locate Searches
Search for a term in pathnames
locate notes
This can return any indexed pathname containing notes. Matches may include filenames, directory names, or both:
/home/alex/notes.txt
/home/alex/Documents/meeting-notes.md
/usr/share/doc/some-package/notes
A simple term can therefore produce many results when it is common in the filesystem.
Search for a specific-looking filename
locate report.txt
This is more specific than searching for report, but it still matches any indexed pathname containing that text. For example, it might match both /home/alex/report.txt and /tmp/old-report.txt.backup, depending on the locate implementation and pattern rules.
Use a more specific path fragment to narrow the search:
locate /home/alex/Documents/report.txt
Adding directory components reduces unrelated matches. When exact matching behavior is important, inspect the command's local manual page with man locate; locate variants differ in how they interpret patterns and regular expressions.
Understanding locate Output
Normal output consists of matching full paths, one path per line. A broad search can return a large number of similarly named files, including paths that are no longer present if the database is stale.
Useful options
| Option | Purpose | Example |
|---|---|---|
-i | Search without regard to letter case, where supported. | locate -i readme |
-c | Print the number of matching pathnames instead of displaying every match. | locate -c log |
-l | Limit the number of displayed results. | locate -l 10 log |
-0 | Separate results with a NUL character, useful when passing names safely to programs that support NUL-delimited input. | locate -0 report.txt |
Options can be combined when supported by the installed locate variant. For example, count broad matches before printing them:
locate -c log
locate -l 20 -i readme
The locate Database
The locate database is an index of file and directory names. locate normally reads this stored index rather than inspecting the live filesystem for every search.
Its location, format, update program, and configuration vary between Linux distributions and locate implementations. The database may be built from selected filesystems and may omit specific directories, bind mounts, or other paths.
Database visibility can also be affected by system policy and user access restrictions. Do not assume that a missing result proves that a path does not exist, and verify sensitive paths before disclosing or acting on search results.
Refresh the Database with updatedb
updatedb creates or refreshes the database used by locate. Many systems run it automatically through a cron job, a system timer, or another scheduled maintenance mechanism.
A scheduled update means that normal searches are fast without requiring an administrator to rebuild the index after every filesystem change. However, the database can remain temporarily stale.
When authorized and appropriate, manually refresh it with:
sudo updatedb
Administrative privileges may be required because database maintenance can read and index areas unavailable to ordinary users. Follow the system's approved administrative process rather than using sudo without authorization.
Handling Missing or Stale Results
A newly created file may not appear in locate because it was created after the last database update. A deleted or renamed file may still appear because the old pathname remains in the index.
Use this diagnostic sequence:
- Confirm the filename and the directory where you expect it to exist.
- Refresh the database if you are permitted to do so.
- Run locate again with the correct name or path fragment.
- Use find when you need an immediate check of the live filesystem.
sudo updatedb
locate new-file.txt
If you cannot refresh the database, search the expected directory directly:
find /home -name 'new-file.txt'
To check whether a path reported by locate still exists, use a live command such as ls or search it with find.
Common causes of unexpected results
| Cause | What it means | Recommended action |
|---|---|---|
| Database has not been refreshed | The index does not include recent filesystem changes. | Run sudo updatedb when authorized, or wait for the scheduled update. |
| File was created after the last update | The file is live, but it is absent from the index. | Refresh the database or use find immediately. |
| Directory or filesystem is excluded | updatedb configuration may intentionally omit the location. | Check the system's indexing policy or search the directory directly with find. |
| Search term does not match the indexed pathname | The name, spelling, capitalization, or path fragment may be wrong. | Confirm the filename and try a more suitable pattern or locate -i. |
| A live search is required | locate cannot guarantee current-state results. | Use find against the required search path. |
locate Compared with find
| Characteristic | locate | find |
|---|---|---|
| Search source | Queries a prebuilt pathname database. | Traverses the live filesystem. |
| Speed | Usually very fast for name lookups. | Can be slower because it examines directories and entries directly. |
| Freshness of results | Depends on when the database was last updated. | Reflects the filesystem as it is searched. |
| Primary search criteria | Names and pathname patterns. | Names, paths, and many file conditions. |
| Metadata filtering | Not its primary purpose and generally limited. | Can filter by permissions, owner, size, modification time, file type, and more. |
| Typical best use | Fast searches when an indexed result is sufficient. | Current-state searches or searches based on metadata. |
For example, use locate when you remember part of a filename:
locate vacation
Use find when you need a live search or a condition such as file type:
find /home -name 'new-file.txt'
find /var/log -type f -name '*.log'
Permissions, Exclusions, and Visibility
updatedb configuration can exclude selected paths, filesystems, or bind mounts. Configuration filenames and locations differ by distribution and locate implementation, so consult the system documentation and local manual pages.
Some databases are built with policies that limit what is indexed or what users can see. A result may also identify a path that you cannot read, while a missing result may reflect an exclusion rather than absence. Treat pathname output as potentially sensitive, especially for home directories, temporary areas, credentials, and application data.
Practical Troubleshooting
locate returns no result for a file just created
The database was probably generated before the file existed. Confirm the name, refresh the database if permitted, and search again. For an immediate live check, use find.
locate shows a path that no longer exists
The database has not caught up with a deletion or rename. Confirm the path with a live filesystem check, then wait for or run the next database refresh.
The search returns too many matches
The term is probably too broad. Use a specific filename or directory fragment, count matches first, or limit the output:
locate -c log
locate -l 20 /home/alex/Documents/report.txt
updatedb fails for a regular user
Database maintenance commonly requires elevated privileges or is managed by the operating system. Use the approved administrative method where authorized, wait for the scheduled update, or use find in the meantime.
Exam-Relevant Summary
- locate searches a prebuilt database of filesystem pathnames rather than normally scanning the live filesystem.
- Its main strength is fast, name-based searching.
- updatedb creates or refreshes the database; scheduled maintenance may use cron or a system timer.
- Recently created, renamed, or deleted files may produce missing or stale locate results.
- Use
-ifor case-insensitive searches,-cto count matches, and-lto limit output where supported. - Use find for current filesystem results and metadata filters such as owner, permissions, size, time, or file type.
- Database exclusions and access policies can affect which paths locate returns.