locate Command in Linux: Cheat Sheet, Syntax & Examples (vs find)

locate Command in Linux: Cheat Sheet, Syntax & Examples (vs find)

The locate command in Linux is used to quickly search files and directories using a prebuilt database instead of scanning the entire filesystem. This makes it significantly faster than the find command for name-based searches. In this guide, you will learn locate syntax, key options, and real-world use cases with a quick cheat sheet.


locate Command Cheat Sheet

TaskCommand
Search file by namelocate filename
Case-insensitive searchlocate -i pattern
Limit resultslocate -n 10 pattern
Count matching fileslocate -c pattern
Show only existing fileslocate -e pattern
Use regex searchlocate -r pattern
Match basename onlylocate -b pattern
Update locate databasesudo updatedb
Show database statisticslocate -S
Use custom databaselocate -d DBPATH pattern

What is locate command in Linux

The locate command in Linux is used to quickly search files and directories based on their names using a prebuilt database. Unlike traditional search tools, it does not scan the filesystem in real time, which makes it extremely fast for large systems.

How locate works using database (mlocate / plocate)

The locate command relies on a database (typically maintained by mlocate or plocate) that stores indexed file paths. This database is updated periodically using the updatedb command.

bash
sudo updatedb

When you run locate, it searches this database instead of scanning directories, resulting in faster output.

Why locate is faster than find command

locate is faster than find because it searches a prebuilt database rather than traversing the filesystem. However, this also means that locate may not show recently created or deleted files until the database is updated.


locate vs find command (Key Differences)

The locate and find commands are both used to search files in Linux, but they work differently and serve different use cases.

When to use locate vs find

Use locate when:

  • You need fast file search based on name
  • You are working with large filesystems
  • Real-time accuracy is not critical

Use find when:

  • You need up-to-date results
  • You want to search by size, permissions, or modification time
  • You need advanced filtering and actions

Limitations of locate command

The locate command has some limitations due to its dependency on a database:

  • May not show recently created files
  • May include deleted files if database is outdated
  • Limited to name-based searches
  • Requires periodic database updates

Install and set up locate command

Before using the locate command, you may need to install and initialize it depending on your Linux distribution.

Install locate on Ubuntu, CentOS, RHEL

Install plocate (modern replacement for mlocate):

bash
sudo apt install plocate     # Ubuntu/Debian
sudo dnf install plocate     # RHEL/CentOS/Fedora

If plocate is not available, install mlocate:

bash
sudo apt install mlocate
sudo yum install mlocate

Initialize and update database using updatedb

After installation, initialize or update the database:

bash
sudo updatedb

This ensures that locate returns accurate results. The database is usually updated automatically via cron jobs.


Search files quickly using locate

The locate command is primarily used for fast file searches based on name patterns.

Search files by name pattern

Search for files matching a specific name:

bash
locate filename

Example:

bash
locate passwd

Perform case-insensitive searches using -i

Use -i to ignore case sensitivity:

bash
locate -i pattern

Example:

bash
locate -i log

Limit search results using -n

Limit the number of results returned:

bash
locate -n 10 pattern

Example:

bash
locate -n 5 *.conf

Improve accuracy of locate results

The locate command relies on a database, so results may sometimes be outdated. These options help improve accuracy and filter relevant results.

Show only existing files using -e

The -e option ensures that only files currently present in the filesystem are displayed. This helps avoid showing deleted files that still exist in the database.

bash
locate -e pattern

Example:

bash
locate -e passwd

Why locate shows deleted files (database issue)

Since locate uses a prebuilt database, it may still show files that have been deleted but are present in the database.

To fix this, update the database:

bash
sudo updatedb

Regular database updates ensure accurate search results.


Perform advanced searches using locate

The locate command supports advanced search capabilities using patterns and regular expressions to refine results.

Use regex pattern with -r

The -r option allows you to search using regular expressions for more precise matching.

bash
locate -r pattern

Example:

bash
locate -r '\.log$'

Match exact file names vs partial matches

By default, locate returns partial matches. To match exact filenames, use regex anchors.

bash
locate -r '/filename$'

Example:

bash
locate -r '/passwd$'

This ensures only exact matches are returned.


Optimize and manage locate database

Managing the locate database helps improve performance and accuracy of search results.

Check database statistics using -S

The -S option displays statistics about the locate database, including number of indexed files and database size.

bash
locate -S

Use custom database with -d

You can specify a custom database instead of the default one.

bash
locate -d DBPATH pattern

Example:

bash
locate -d /tmp/custom.db test

Reduce noise using --quiet option

The --quiet (or -q) option suppresses error messages such as permission denied.

bash
locate -q pattern

This helps produce cleaner output.


Frequently Asked Questions

1. What is locate command in Linux?

locate is a Linux command used to quickly find files using a prebuilt database instead of scanning the filesystem.

2. Why is locate faster than find command?

locate searches a prebuilt database, while find scans the filesystem in real time, making locate significantly faster.

3. How to update locate database?

Use sudo updatedb to manually update the locate database.

4. Why locate does not show recent files?

locate relies on a database that may not be updated immediately, so recently created files may not appear.

5. What is the difference between locate and find command?

locate is faster but may show outdated results, while find is slower but always shows real-time and accurate results.

Summary

The locate command is a fast and efficient tool for searching files in Linux using a prebuilt database. While it offers excellent performance compared to find, it requires periodic database updates to ensure accuracy. By understanding key options like -i, -n, -e, and -r, you can perform both simple and advanced file searches effectively.


Official Documentation

For more details, refer to the official documentation:

locate command man page

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.