locate command in Ubuntu
Commands

Use Locate Command to Quickly Find Files in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

Find is the go-to command for searching for files in the Linux terminal. It is extensive and you can use it to find files based on numerous criteria.

However, find is not the only tool for finding files in the terminal. There is a locate command as well.

But do you need another utility? The find command works just fine, right? Let me tell you why you may prefer locate over find at times.

Locate does not have as wide a range of options. Which makes it simpler to use. It utilizes its local database which makes it faster than find.

In other words, when you are just looking for a file by its name and you are not sure about the location, locate comes to the rescue.

Sounds interesting? Let's see about using locate on Ubuntu.

Install locate command on Ubuntu

The sad part is that locate utility is not a part of default packages in Ubuntu and needs manual installation by the given command:

sudo apt install locate

In Ubuntu 22.04, you can install it from plocate package (a newer, faster implementation of locate):

sudo apt install plocate

And then you'd need to update its local database otherwise, you'd get no response from any query.

sudo updatedb
⚠️
The local database does not get updated frequently and the chances of recently created/modified files won't make it to the search results are pretty high. So updating the database manually is the only choice you're left with when reflecting recently made changes is crucial.

Using locate command

First, let me introduce the simple syntax of locate command:

locate [options] [pattern]

Here,

[options] are going to let you decide what kind of search you want to go with such as whether it is a database, want to go with whole name, and many others.

[pattern] allows you to search for specific patterns such as the only search for the .txt files and so on.

Now, let's jump to the examples.

Find files with specific names

This is the easiest way to find files if you're aware of the file name.

locate File_name
Find files by name using locate
Finding files by filename

As you can see, it brought all the filenames starting with Helloworld and if you're looking for a way to find files with exact names, you can skip to the later section.

Get a free, secure, and private email with Proton Mail
Proton Mail is the world’s largest secure email service with over 70 million users. Available on Web, iOS, Android, and desktop. Protected by Swiss privacy law.

Ignore case-sensitive filenames

To widen your search, you can use -i option to ignore cases in file names.

locate -i File_name
Ignore case sensetive file names during search
Ignore case-sensitive outputs

Use regex

You can use regular expressions to find files with matching patterns in the name.

locate -r File_name

If you give it the file name instead of a pattern, it will show files with the exact file name.

Search files with exact filename in terminal using locate command
Searching files with exact filename

Avoid showing deleted files in the result

As locate utilizes a local database, I often myself in situations where it brings deleted files in search results as given:

Shows deleted previously deleted files too

In that case, you can force locate utility to only search for those files that are currently available using -e option.

locate -e File_name
Filter deleted files from search results using locate command
Filtering deleted files from search results 

You won't find Helloworld.txt in the home directory when I used the -e option in the 2nd command of the above snapshot.

Count the search results instead of showing files

If you're reading numerous amount of files, before attempting to search, having an idea of the exact number will always be beneficial.

Still confused about its use case? Let me show you why it is crucial.

I'd be printing the number of .txt files available in my system using -c option. Sounds ridiculous right?

locate -c .txt
count search results in locate command
Counting search results before attempting to search

2795 results. Enough to make anyone mad, right?

Discover how easy it is to run Windows apps on Mac, Linux and ChromeOS with CrossOver.
Don’t buy a Windows license, don’t reboot or use a virtual machine until you try CrossOver for Mac, Linux, or ChromeOS. Download a free 14 day trial now and get your Windows apps running on Mac and Linux.

Find or locate?

That's entirely up to you. The locate is good at quickly finding files based on their name. The find command is good at locating files based on name, size, modification, type and whatnot.

Both commands have their usage. When you want a simple file search by its name, locate is sufficient. If you want something more than that, find is your ally.

How to Find Files in Ubuntu Command Line [4 Ways]
Looking for a specific file in the terminal? Here are all the essentials you need to know about finding files in the Ubuntu command line.

Let me know if you have any questions.