mlocate

file managementlinux
The mlocate command is one of the most frequently used commands in Linux/Unix-like operating systems. mlocate The mlocate command (Merging Locate) is a secure implementation of the locate command that maintains file ownership/permission information and merges new file entries incrementally. It provides a fast way to search the filesystem by using a previously built database.

Quick Reference

Command Name:

mlocate

Category:

file management

Platform:

linux

Basic Usage:

mlocate [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

mlocate [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
-n, --limit N Same as -l
-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
-q, --quiet Write no messages about errors encountered
-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 mlocate command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

mlocate document.pdf

Find all paths containing "document.pdf" in their name.

mlocate -i report

Find all files with "report" in their name, ignoring case (finds Report, REPORT, report, etc.).

mlocate "*.jpg"

Find all JPG images on the system.

Advanced Examples:

mlocate -c "*.pdf"

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

mlocate -l 5 "*.conf"

Limit the output to just 5 config files.

mlocate -r "^/etc/.*\.conf$"

Use a regular expression to find configuration files in the /etc directory.

mlocate -b "bash"

Match only the basename (filename without path) containing "bash".

mlocate -e -i "readme"

Find all readme files (case insensitive) that still exist (haven't been deleted 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 mlocate package provides both the updatedb and locate commands
  • It's the most common locate implementation on modern Linux distributions
  • mlocate maintains file permission information, making it more secure than older locate implementations
  • The database is typically stored at /var/lib/mlocate/mlocate.db
  • mlocate updates its database incrementally, merging new data with existing data
  • Users can only see files in the locate results that they have permission to access

Security Features:

  • Permission checking: Only shows files that the user has permission to see
  • Group restrictions: Database can only be read by members of the mlocate group
  • Incremental updates: Preserves permissions when updating the database
  • Pruning paths: Configurable to exclude sensitive directories

Common Use Cases:

  • Quickly finding files when you only remember part of the name
  • Locating configuration files across the system
  • Finding all files of a certain type (by extension)
  • Security-conscious file searching where permissions matter
  • System administration tasks requiring file location

Related Commands:

  • updatedb - Update the file name database used by locate
  • locate - The generic command that often points to mlocate
  • slocate - An older security-enhanced locate variant
  • find - More powerful but slower file search tool
  • which - Locate executables in the system PATH

Configuration:

  • /etc/updatedb.conf: Controls what files are indexed and excluded
  • PRUNE_BIND_MOUNTS: Whether to skip bind mounts (usually "yes")
  • PRUNEFS: Filesystem types to skip (e.g., "NFS", "SMBFS")
  • PRUNENAMES: Directory names to skip (e.g., ".git", ".svn")
  • PRUNEPATHS: Paths to skip (e.g., "/tmp", "/var/cache")

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 mlocate 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 mlocate command works in different scenarios.

$ mlocate
View All Commands