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

    File search

    Search for files and directories based on name

  • 2

    Quick lookup

    Find files quickly without traversing the filesystem

  • 3

    Scripting

    Use in shell scripts to search for files programmatically

  • 4

    File management

    Locate and manage files in the filesystem

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 the -i option for case-insensitive searching

2

Use the -l number option to limit the number of results

3

Use the -e option to only display entries that exist at the time locate is run

4

Use the -r regexp option to search using a basic regular expression

5

Use the -d path option to specify a custom database path

Common Use Cases

File search

Search for files and directories based on name

Quick lookup

Find files quickly without traversing the filesystem

Scripting

Use in shell scripts to search for files programmatically

File management

Locate and manage files in the filesystem

Data recovery

Recover lost or deleted files

Related Commands

These commands are frequently used alongside locate or serve similar purposes:

Use Cases

1

File search

Search for files and directories based on name

2

Quick lookup

Find files quickly without traversing the filesystem

3

Scripting

Use in shell scripts to search for files programmatically

4

File management

Locate and manage files in the filesystem

5

Data recovery

Recover lost or deleted files

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