Skip to main content

Module 1: File & Directory Management

Lesson 18: locate Command

In this lesson, you'll learn how to use the locate command to quickly search for files and directories in Linux.

Linux, like almost any other operating system, utilizes several mechanisms to answer search queries for users.

Two of the most popular file-searching utilities accessible to users are called find and locate.

The locate utility works better and faster than its find counterpart because instead of searching the filesystem when a file search is initiated, something find does - locate would look through a database, which contains bits and parts of files and their corresponding paths on your system.

Let's discuss ten simple locate commands to aid you in becoming more productive with your Linux machine.

locate Command Syntax

locate [OPTIONS] PATTERN

locate Command Options

Option Description
-n Limit the number of search results returned
-c Display only the count of matching entries
-i Ignore case sensitivity in search queries
-e Display only files that currently exist on the system
-0 Separate output entries with ASCII NUL instead of newline
-S Display statistics about the mlocate database
-q Suppress error messages
-d Use a different mlocate database location

1. Search for a File Using locate

Firing the locate command to look for a file is pretty straightforward. All you need to do is type:

$ locate LAMP-Setup.odt
/home/ubuntu/LAMP-Setup.odt
/home/ubuntu/TecMint.com/LAMP-Setup.odt

2. Limit Search Results to a Specific Number

You can limit your search returns to a required number to avoid redundancy with your search results using the -n option.

For example, if you want just 20 results from your queries, you can type the following command:

$ locate "*.html" -n 20
/home/ubuntu/.config/google-chrome/Default/Extensions/aapocclcgogkmnckokdopfmhonfmgoek/0.9_0/main.html
/home/ubuntu/.config/google-chrome/Default/Extensions/aohghmighlieiainnegkcijnfilokake/0.9_0/main.html
/home/ubuntu/.config/google-chrome/Default/Extensions/felcaaldnbdncclmgdcncolpebgiejap/1.1_0/main.html
/home/ubuntu/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/forge.html
/home/ubuntu/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/src/popup.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/additional-feature.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/background.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/edit.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/help.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/options.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/popup.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/purchase.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/upload.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/oauth2/oauth2.html
/home/ubuntu/.config/google-chrome/Default/Extensions/nmmhkkegccagdldgiimedpiccmgmieda/1.0.0.2_0/html/craw_window.html
/home/ubuntu/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_route_details.html
/home/ubuntu/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/feedback.html
/home/ubuntu/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/devices.html
/home/ubuntu/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/index.html
/home/ubuntu/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/offers.html

The results will show the first 20 files that end with .html.

3. Display the Count of Matching Entries

If you want to display the count of all matching entries of file "tecmint", use the locate -c command.

$ locate -c [tecmint]*
1550

4. Ignore Case Sensitivity in locate

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’