locate

file managementlinux
The locate command is one of the most frequently used commands in Linux/Unix-like operating systems. locate The locate command quickly finds files by name on a Linux system. It searches a database built by updatedb rather than scanning the filesystem in real-time, making it much faster than find for basic file name searches.

Quick Reference

Command Name:

locate

Category:

file management

Platform:

linux

Basic Usage:

locate [options] [arguments]

Common Use Cases

  • 1

    Fast file searching

    Find files anywhere on the system by name without scanning the filesystem in real-time

  • 2

    Pattern matching

    Search for files using wildcards, regular expressions, or partial names

  • 3

    System exploration

    Quickly discover where programs, libraries, or configuration files are installed

  • 4

    Filename verification

    Check if certain files exist on the system and where they are located

Syntax

locate [OPTION]... PATTERN...

Options

Option Description
-A, --all Display all matching entries, including those on hidden paths
-b, --basename Match only the basename of file paths
-c, --count Print only the number of found entries
-d, --database DBPATH Use a custom database path instead of the default
-e, --existing Only print entries for currently existing files
-i, --ignore-case Ignore case distinctions when matching patterns
-l, --limit N Limit output to N entries
-m, --mmap Ignored, for backward compatibility
-P, --nofollow Don't follow trailing symbolic links when checking file existence
-0, --null Separate output with null characters, not newlines
-S, --statistics Print statistics about the database
-r, --regexp REGEXP Search using a basic regular expression
--regex Patterns are extended regular expressions
-w, --wholename Match the whole path name (default)

Examples

How to Use These Examples

The examples below show common ways to use the locate command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

locate filename.txt

Find all paths that contain "filename.txt" in their name.

locate -i readme

Find all files with "readme" in their name, ignoring case (finds README, Readme, readme, etc.).

locate "*.conf"

Find all configuration files that end with .conf.

Advanced Examples:

locate -c "*.jpg"

Count the number of jpg files in the database instead of displaying them.

locate -l 10 "*.mp3"

Limit the output to just 10 results when searching for mp3 files.

locate -r "\bpasswd$"

Use a regular expression to find files named exactly "passwd".

locate -b "\bgrep"

Match only the basename (no path) containing "grep".

locate -e firefox

Only show files that still exist (not removed since the database was updated).

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The locate command is significantly faster than find for basic name-based file searches
  • It depends on a database created and updated by the updatedb command
  • The database is typically updated daily via a cron job, so newly created files may not be found until the next update
  • You can manually update the database by running sudo updatedb
  • By default, locate shows all files containing the pattern, not just exact matches

Database Location:

  • /var/lib/mlocate/mlocate.db: Default database location on many Linux distributions
  • /var/lib/locate/locate.db: Used by older locate implementations
  • ~/.locate.db: Sometimes used for user-specific databases

Common Use Cases:

  • Quickly finding configuration files on the system
  • Locating installed programs or libraries
  • Finding files when you remember only part of the file name
  • Searching for files with specific extensions across the entire system
  • Checking if certain files exist on the system

Related Commands:

  • updatedb - Update the file name database used by locate
  • find - More powerful but slower file search tool
  • whereis - Locate binary, source, and manual files for commands
  • which - Show the full path of commands
  • grep - Search file contents (whereas locate searches file names)

Implementations:

  • mlocate: Modern locate with enhanced security features (most common)
  • slocate: Secure locate, considers file permissions
  • GNU locate: Part of the GNU findutils package
  • plocate: New implementation with faster searching

Tips & Tricks

1

Use -i for case-insensitive searches: locate -i readme finds README, Readme, readme, etc.

2

Limit results with -l: locate -l 10 "*.mp3" shows only the first 10 matches

3

Use wildcards for pattern matching: locate "*.conf" finds all .conf files

4

Count matches instead of listing them: locate -c "*.jpg" shows the total number of matches

5

Check if files still exist: locate -e pattern only shows files that haven't been deleted since the last updatedb

6

Find exact filenames with regex: locate -r "\bexactname$" matches only files named exactly "exactname"

7

Search only for base filenames: locate -b pattern ignores the directory path in matches

Common Use Cases

Fast file searching

Find files anywhere on the system by name without scanning the filesystem in real-time

Pattern matching

Search for files using wildcards, regular expressions, or partial names

System exploration

Quickly discover where programs, libraries, or configuration files are installed

Filename verification

Check if certain files exist on the system and where they are located

File categorization

Find all files of a specific type or extension across the filesystem

Related Commands

updatedb

updatedb

View command

find

find

View command

which

which

View command

whereis

whereis

View command

slocate

slocate

View command

mlocate

mlocate

View command

grep

grep

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the locate command works in different scenarios.

$ locate
View All Commands