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:
Shell
You may have it installed by default. You can check that with:
Shell
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
‘ and really quite simple. It does what you tell it to do, nice and easy.
For example, it may return a lot of results:
Shell
Or it can be quite targeted:
Shell
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:
Shell
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:
Shell
You can limit the number of responses with ‘-n
‘ easily enough:
Shell
The locate command defaults to being case-sensitive, but you can change that behavior with the -i
flag:
Shell
Finally, you can check the database statistics with this command:
Shell
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.