Cybersecurity Updates & Tools

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database instead of scanning the filesystem in real time, making it significantly faster than find for simple name lookups.

How to Install and Use the locate Command in Linux

Two implementations exist. plocate is the modern version: it uses a compressed index, consumes less memory, and returns results faster. On Ubuntu 22.04+ and Debian 12+, plocate is the default. mlocate is the older version still found on some systems.

Install using your package manager:

bashsudo apt install plocate         # Ubuntu 22.04+, Debian 12+sudo apt install mlocate         # older Ubuntu and Debian releasessudo dnf install plocate         # Fedora, RHEL

Once installed, run a search:

bashlocate .bashrc

When the pattern contains no wildcard characters, locate treats it as a substring and implicitly searches for *pattern*. The command above returns every path that contains .bashrc anywhere in the full path — /etc/bash.bashrc/usr/share/doc/adduser/examples/bash.bashrc, and your own ~/.bashrc.

Results are restricted to files the running user has read permission to. A regular user will not see /root/.bashrc in the output.

Quote glob patterns. When using wildcards, always wrap the pattern in single quotes to stop the shell from expanding it before locate receives it:

bashlocate '*.md'locate -n 10 '*.py'    # limit to the first 10 results

Narrow Results: Basename Match, Case, Count, and Existing Files

Case-insensitive search with -i:

bashlocate -i readme.md

This returns readme.mdREADME.md, and ReadMe.md.

Count matching entries without printing paths:

bashlocate -c .bashrc

Useful in scripts when you only need a number, not a list.

Match only the filename with -b (basename). By default, locate matches the pattern against the full path. -b restricts matching to the last component only:

bashlocate -b '\config.yaml'

The leading backslash changes the behavior: it tells locate to match the exact basename string rather than treating it as a substring. Without the backslash, locate -b config.yaml would still match any basename that contains config.yaml as a substring — for example, my-config.yaml or config.yaml.bak.

Show only existing files with -e. Since locate searches a database, it can return paths for files deleted since the last database update. The -e flag verifies existence at query time and filters out stale entries:

bashlocate -e '*.json'

For more complex searches, use -r for basic regular expressions:

bashlocate --regex -i "(\.mp4|\.avi)"

Keep the Database Fresh and Know When to Use find

The database is built by updatedb and refreshed automatically once a day. On plocate systems, a systemd timer handles this. On older mlocate systems, a cron job in /etc/cron.daily/mlocate runs the update.

Files created after the last update will not appear in locate results. Run the update manually before searching for recently created files:

bashsudo updatedb

If you search for a file you just deleted and it still appears in the results, that is also a stale database — use -e to filter, or refresh with updatedb.

locate vs find. Use locate for fast, name-based lookups of files you know exist. Use find when you need to search by attributes — file size, permissions, modification time, owner, or content — or when you need guaranteed real-time accuracy. For anything beyond a name match, find is the right tool.

Run sudo updatedb before searching for recently created files and use -e when results might be stale. Use -b '\filename' when you want an exact name match, not a path substring. Leave a comment below if you run into any issues.