Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use the locate Command for Fast File Searching in Linux

Published

LinuxLinux and Commands

Searching for files on a Linux system can be challenging, especially as your directory structure grows and files accumulate over time. While commands like find are powerful, they can be slow because they scan directories in real time. The locate command offers a much faster alternative by using a prebuilt database, enabling you to find files by name almost instantly.

This article introduces the locate command, explains how it works, guides you through installation and configuration, and demonstrates practical search techniques. You will learn how to update the database, filter results, use wildcards, and combine locate with other Linux tools for efficient file management.

Prerequisites

Before you begin, ensure you have:

  • Access to a Linux machine or terminal
  • Sudo privileges (for installing software and updating the locate database)
  • Basic familiarity with the Linux command line

Introduction to locate

The locate command is a fast file search utility that allows you to find files and directories by name. Unlike find, which searches the filesystem in real time, locate searches through a database that is periodically updated with the paths of all files on your system.

How Does locate Work?

The locate command depends on a database file, usually /var/lib/mlocate/mlocate.db, which contains a list of all files and directories on the system.

  • The database is created and updated by the updatedb utility, which is typically run automatically as a scheduled job (cron).
  • When you run locate search_term, the command quickly matches your search term against the prebuilt database and returns results almost instantly.

Installing locate

On many Linux distributions, the locate command may not be installed by default. If you run locate and receive a “command not found” error, install the correct package for your system.

On Ubuntu/Debian

$ sudo apt update
$ sudo apt install mlocate

On CentOS/RHEL

$ sudo yum install mlocate

On Fedora

$ sudo dnf install mlocate

Some older distributions use the locate package. The mlocate package is now most common and recommended.

After installing, verify by running:

$ locate --version

Building and Updating the locate Database

Step 1: Before your first search, build the database with:

$ sudo updatedb

This command scans your filesystem and creates the database, which may take a few minutes on a large system.

Step 2: The database is usually updated automatically each day via a cron job. If you add or remove files and need the latest results immediately, run:

$ sudo updatedb

Basic Usage of locate

The most basic use is to provide a filename or partial name as an argument.

Example: Find a File by Name

$ locate report.txt

You might see results like:

/home/alex/Documents/report.txt
/home/alex/backup/report.txt
/tmp/report.txt

Example: Find Files by Pattern

$ locate notes

This command lists all files and directories with “notes” in their names.

Example: Locate an Executable

$ locate bin/python3

The command searches for any path containing “bin/python3”.

Using locate with Wildcards and Patterns

Unlike many search tools, locate automatically matches substrings, so you do not need to use * at the beginning or end of your pattern. However, you can use wildcards within patterns for more precise matching.

Example: Find All PDF Files

$ locate .pdf

Lists all PDF files indexed in the database.

Example: Search for Config Files

$ locate .conf

Shows all .conf configuration files.

Example: Restrict Search to a Specific Directory

$ locate /etc/passwd

Finds all matches for “passwd” under /etc.

Limiting the Number of Results

Step: If your search returns too many results, use -n (number) to limit the output:

$ locate -n 10 log

Displays only the first 10 matches.

Case-Insensitive Searches

Step: Use -i for case-insensitive searches:

$ locate -i Resume

This command finds Resume.docx, resume.txt, and RESUME.pdf

Filtering Output with grep

Step: Combine locate with grep for more advanced filtering. For example, to show only .sh files in your home directory:

$ locate .sh | grep "^/home/alex/"

Or to find log files in /var/log related to errors:

$ locate .log | grep "/var/log/" | grep -i error

Excluding Results

Step: Use grep -v to exclude certain matches:

$ locate .bak | grep -v "/backup/"

Lists all .bak files except those in /backup/.

Keeping the locate Database Updated

Step: Linux systems typically update the mlocate database automatically via a cron job. To check how it’s scheduled:

$ cat /etc/cron.daily/mlocate

If you want to update the database more frequently after many file changes, run:

$ sudo updatedb

Troubleshooting locate

Database Not Updated

Step: If new files are not appearing in your searches, update the database:

$ sudo updatedb

Permission Issues

The mlocate database only shows files you have permission to access. If you need to index system-wide files, run updatedb as root.

Excluded Directories

Some directories (such as /tmp, /proc, and network mounts) are excluded by default. Adjust /etc/updatedb.conf if you need to include or exclude specific locations.

Searching for Directories Only

$ locate notes | grep '/$'
$ locate notes | xargs -d '\n' file | grep directory

Advanced Usage and Options

Show Only Existing Files

$ locate -e .txt

Count Matches

$ locate -c .log

Search for Patterns with Regular Expressions

$ locate .log | grep -E 'access|error'

Using locate in Scripts

#!/bin/bash
for file in $(locate .log | grep "^/home/alex/logs/"); do
    cp "$file" /home/alex/archive/
done

Configuring locate

Excluding Directories

Edit /etc/updatedb.conf:

PRUNEPATHS="/tmp /media /mnt /proc /sys /dev"

After changing, update the database:

$ sudo updatedb

Privacy and Security

mlocate indexes only files accessible to the user running the search, improving privacy and security compared to older versions like slocate.

Real-World Examples

  • Find All SSH Keys:
    $ locate id_rsa
  • Track Down Large Video Files:
    $ locate .mp4
    $ locate .mkv
  • Audit for Duplicate Backups:
    $ locate backup.zip
  • Locate All Configuration Files for nginx:
    $ locate nginx | grep .conf

locate vs find

Feature locate find
Speed Instant (uses a database) Slower (scans filesystem in real time)
Freshness May be out of date Always current
Search type Name-based, pattern matching Name, attributes, time, size, etc.
Syntax Simple More complex, powerful

Best Practices

  • Use locate for fast, routine file searching by name or extension.
  • Run sudo updatedb after large file changes for accurate results.
  • Combine with grep for targeted filtering.
  • Use find for more detailed, real-time searches.
  • Exclude sensitive or volatile directories in /etc/updatedb.conf for privacy and performance.
  • Remember that results may not reflect the very latest changes until the database is updated.

Conclusion

The locate command is a powerful and efficient tool for searching files by name on Linux. By leveraging a prebuilt database, locate delivers results almost instantaneously, making it ideal for everyday file management, troubleshooting, and auditing. With regular database updates and thoughtful use of patterns and filters, you can integrate locate into your workflow for a faster, more organized Linux experience.

Practice using locate alongside other search and management tools to fully harness the power and convenience it brings to file searching on Linux.

Related guides

Account services are unavailable until the production backend is configured.