Tutorials

Let’s Install And Use ‘locate’

Today, we’re going to learn to install and use locate and we’ll even cover some basic usage. If you’re unfamiliar with locate, it does exactly what you’d expect – it helps you locate files on your computer. Read on for a basic understanding.

The locate command is a terminal-based command, a text way to find files on your computer. It can be a pretty useful command – with some advanced usage – but we’ll just cover some basics.

The locate command is actually installed by installing ‘mlocate‘. So, let’s just get that out of the way. Depending on the distro you use, it’ll likely be in your default repositories and you can install it much like you’d install any other software. For example, in a distro that uses apt you’d install it with:

sudo apt install mlocate

You may have it installed by default. You can check that with:

locate --v

If that spits out a version and some version information, you’ve already got it installed and there’s no need to install it.

Anyhow, with that accomplished, let’s learn a bit about how to …

Use Locate:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Of course, you’d have had to open the terminal in the preamble, but I  might as well tell you again. With the terminal open, you can check the man page for locate. You’ll see it does exactly what you’d expect it to do:

locate – find files by name

To use the locate command, it’s quite simple. It’s ‘locate <flags> <file_name>‘ and really quite simple. It does what you tell it to do, nice and easy. 

For example, it may return a lot of results:

locate firmware

Or it can be quite targeted:

locate .bash_aliases

However, it needs a database to work from. If you’ve just installed the mlocate package, you’ll need to update the database. Hmm… I probably could have mentioned this sooner. Oh well… You’ll find it if you need it. It pays to read all the words, folks!

To update said database, it’s this command:

sudo updatedb

Now, there are a couple of useful flags. We’ll cover a few. You can just return a number of how many files match the description with the -c flag:

sudo locate -c <file_name>

You can limit the number of responses with ‘-n <number>‘ easily enough:

sudo locate -n 10 <file_name>

The locate command defaults to being case-sensitive, but you can change that behavior with the -i flag:

sudo locate -i <file_name>

Finally, you can check the database statistics with this command:

locate -S

While there are other options for the locate command, including using it with other commands, those are the options I find myself using more often than not. I suspect those will be the most often used options when you too make use of the locate command. Be sure to check ‘man locate‘ for more information.

Closure:

See? It’s a quick and easy article about how to use locate to find files on your Linux computer. It’s not terribly difficult and it’s a handy command to have learned. As mentioned, the man page has even more options for you to use the locate command. Feel free to check ’em out.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

View Comments

  • While you can manually run updatedb, most Linux distributions run it on a schedule, typically every 24 hours. If you want to include new or updated files, you'll need to run updatedb to pick them up before the scheduled update.

    • Indeed, or you may need to run it if you've just installed it.

      That's what I was going for in my description, though it's probably as clear as mud. Someone more adept really should be writing some of these articles!

Recent Posts

Legitimate Reasons To Not Use Linux

Today's article is going to be something my regular readers wouldn't expect as we discuss…

2 days ago

Meta: The State Of Linux Tips #23

Today's article is just a meta article, an article that I fail to write every…

4 days ago

Is Your Storage Drive An SSD Or An HDD?

Today's article aims to answer a simple question, simply is your storage drive an SSD…

6 days ago

Monitor System Resources With “Resources”

Today is one of those days when I'd like to introduce you to new software…

1 week ago

Short: The Halt Command

Today's article is intentionally short because we're simply going to learn about the basics of…

1 week ago

Installing Flatpaks In Linux

Today we're going to have a pretty easy article where you'll learn about installing Flatpaks…

2 weeks ago