Locate Command in Linux: Find Files by Name

Locate Command in Linux: Find Files by Name

To find a file by name, you can search for part of its filename instead of starting with its full path. The locate command searches a filename database for matching paths.

I’ll walk through searching by name and checking whether a result may come from an out-of-date index.

TL;DR

The locate command searches a filename database, so it returns paths quickly but can miss new files or show removed ones until its index is refreshed.

  • Search by name with locate report.pdf.
  • Use updatedb to rebuild or refresh the database when your implementation supports it.
  • Use find when you need a live search or directory-specific filters.

What Is the locate Command in Linux?

locate finds pathnames by matching your search text against a database created by an indexing tool such as updatedb. It reads that index instead of walking the directory tree for each query.

That design makes repeated name searches fast, but the index is a snapshot. A file created after the last update may not appear, and a deleted file can remain in results. The command name can refer to different implementations, including GNU locate and plocate.

locate --version
man locate

The version output identifies the implementation when supported. The manual documents the options installed on that machine. Distribution package names vary, so search your distribution’s package index for plocate or locate rather than assuming mlocate is current.

How to Use the locate Command Step by Step

Use a plain search term for a first lookup, then narrow it with options that your local manual documents. Refreshing the index may need administrator privileges, so run that step only on a machine you administer.

Step 1: Search for a file or directory name

Pass a distinctive part of the name as the search term. Locate commonly matches text within the pathname, so results can include names containing the text rather than an exact basename match.

locate report.pdf

Use the -b option when the search should match the basename, the final filename component, rather than any part of the full path. For instance, locate -b ‘\\report.pdf’ limits the match to a basename ending in that name in implementations that document this syntax.

Step 2: Narrow or count the matches

When a broad search returns too many paths, use options such as -i for case-insensitive matching, -c to count matches, or -n to limit displayed results where supported by the installed implementation.

locate -i -n 10 notes

This asks for up to ten case-insensitive matches. Use the local manual to check option support because locate implementations do not expose an identical interface.

Step 3: Refresh the filename database

If a recently added path is missing, refresh the index with the updater provided by your implementation. GNU findutils documents updatedb.

Plocate also provides an updater, often named updatedb. The command may require elevated privileges because it builds a system-wide index.

sudo updatedb

The command is for an administrator to run on their own system. I did not run it because it changes the system index and requires root privileges. A scheduled update may already maintain the database, but timing and exclusions depend on the distribution’s configuration.

When Should You Use locate or find?

Choose locate for quick searches across indexed pathnames, and find when the search must inspect current filesystem state or apply conditions while traversing a chosen directory.

NeedUseReason
Quick lookup by namelocate report.pdfSearches the existing index
Include a file created just nowfind /path -name ‘report.pdf’Walks the live directory tree
Verify an indexed path still existslocate -e report.pdf, if supportedFilters results against current existence
Inspect file type, size, or modification timefindCan test filesystem attributes during traversal

For a live search, find /home/user -name ‘report.pdf’ scopes the search to that directory.

The trade-off is traversal work, while locate’s results depend on the index contents and its access rules.

Common Errors When Using locate

For this refresh, the locate executable was absent, so I could not capture its output. The article therefore relies on the GNU and plocate documentation for command behavior.

When locate is missing, check whether an implementation is installed. When results look stale, an administrator can refresh the database, while a live search can use find:

command -v locate
find /path -name 'report.pdf'

If an option is rejected, check the installed locate manual because GNU locate and plocate differ. Use locate -e only when that manual documents it.

Conclusion: Check the Index Before Trusting a Path

Locate is a useful filename search when its database is current. Refresh the index for new paths, verify uncertain results, and switch to find when the task depends on live filesystem attributes or a narrow search root.

For live traversal, see the find command guide. The man command guide explains how to read the local reference for locate.

FAQ

These answers cover the common questions about installation, freshness, and when to use another search command.

How does locate work in Linux?

It matches a search pattern against a filename database built by an updater such as updatedb. Results can be stale until the database is refreshed.

How do I update the locate database?

Run the updater supplied by your locate implementation, commonly updatedb. It may require administrator privileges and its behavior depends on the implementation and distribution.

Why does locate show a file that was deleted?

The indexed database can retain a pathname after the file has been removed. Refresh the index or use an existence-check option supported by your implementation.