How to use the Linux locate Command, With Examples

Linux locate Command

Here’s another tool for your Linux file searching toolbox – the locate command. This explains how to use it.

The locate command is specifically made for finding files and folders by their name. It’s easy to use and can search for files using patterns. This is helpful if you are looking for files with a specific type/file extension.

Installing locate

locate may not be installed by default on your system.

To install on Debian/Ubuntu-based distributions, run:

sudo apt install mlocate

Or on Redhat/CentOS/Fedora:

sudo yum install mlocate

Linux locate Command Syntax

The locate command is fast. That’s because instead of searching the file system directly, it searches a database of files that is periodically updated. This makes the search much quicker.

It does, however, mean that if a file has been moved, renamed, or removed since the last time the database was refreshed, it will still appear in the results.

By default, the database used by locate to find files will be scheduled to be updated daily, but it’s possible to trigger a manual update if you have re-arranged your files and want to perform a search on an up-to-date record.

Updating the Database

To update the database used by locate to find files, run:

sudo updatedb

Locating Files

The syntax for finding files using locate is as follows:

locate OPTIONS PATTERN

Note that:

  • OPTIONS are optional and can be supplied from the below table to alter the default behavior of the locate command
  • PATTERN is the pattern file names must match to be considered a match for the search
  • Results for matching files will be from the entire filesystem by default – not just the current working directory
  • If no database has been generated for the locate command to use, you may be prompted to generate one
  • The locate command will return a status code of 0 if a matched file is found and 1 if no file is found or an error occurs.

Command Options

Here are some of the commonly used options for the locate command, straight from the official docs:

-b Match only the base name against the specified patterns.
-c Instead of writing file names on standard output, write the number of matching entries only.
-e Print only entries that refer to files existing at the time locate is run.
-i Ignore case distinctions when matching patterns.
-l Exit successfully after finding LIMIT entries. If the –count option is specified, the resulting count is also limited to LIMIT.
-P When checking whether files exist (if the –existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links to be reported like other files.
-q Write no messages about errors encountered while reading and processing databases.
-r Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.
–regex Interpret all PATTERNs as extended regexps.

To view a full list of options, you can view the user manual for the locate command by running:

man locate

locate Command Examples

The most basic usage for the locate function is to find a file matching a given name:

locate filename

A list of file paths for each matching file in the file system will be returned.

As explained above, the filesystem itself is not searched by locate – but a database that indexes the filesystem and is updated periodically is searched. This keeps things fast but may cause issues if you’ve just finished moving a bunch of files around and the database hasn’t yet been updated.

The -e option will force locate to check that a file still exists in the file system before adding it to the result list:

locate -e filename

This does not solve the issue of newly created or moved files not being in the results (or appearing at their old location) but resolves the issue of removed files appearing in the results.

Patterns can be supplied so that you can easily search by file extension:

locate *.mp3

The above example will return all mp3 files found in the database.

The locate command is case-sensitive by default – this can be overridden using the -i option:

locate -i FILENAME

You can also count the number of matching files using the -c option:

locate -c filename

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *