And Still Another Way To Find Files By Extension

Today we will cover a subject we’ve covered before, as we discuss yet another way to find files by extension. This is Linux! We have options! There are so many ways to find files by extension – and this is one of the more interesting of those ways. So, if you want to find files by extension, this might be the article for you.

You might be interested in previous articles that covered this topic:

Another Way To Locate Files By Extension
Yet Another Way To Find Files By Extension

You can search to find more. This is a task that has all sorts of ways to accomplish it. Some ways are easier than others. I’d say that the method we use to find files by extension in this article will be fairly easy. There’s not a whole lot to it.

We will be installing software. We’ll be installing mlocate but the man page will refer to it as plocate while the command we’ll be using will just be locate. Sound confusing? Well, it is. Don’t worry, the directions are still simple.

mlocate:

We’ll be installing mlocate as our tool to find files by extension. We’ll do this in the terminal, a fairly universal way to do things. Pretty much every distro is going to have at least one terminal available. You can usually open your default terminal by pressing CTRL + ALT + T on your keyboard.

When you do get mlocate installed, you can check the man page with ( man locate). There, you’ll see that locate is described like so:

plocate – find files by name, quickly

Yes, it has now been called mlocate, plocate, and locate. No, I do not know why. I do not think I’ll try to find out why. This is just one of those things you sort of accept and move on. Please feel free to research this and leave a comment. I’m not terribly curious, but other people might be.

With your terminal open, we can get mlocate installed with one of the following commands:

Debian/Ubuntu:

Arch/Manjaro/etc:

RHEL/CentOS/etc:

OpenSUSE/GeckoLinux/etc:

There are other package managers. If your package manager isn’t covered, just go ahead and search for “mlocate” and you’ll likely find that it’s available by default.

Configure mlocate:

Now that you have mlocate installed on your system, you need to update the database. The locate command requires a database. On slow systems with many files, this can take a few seconds. It should not take long on a modern system.

The only command you need to run at this stage is this:

Let that finish. It should not take very long. If it’s taking a long time, something might be amiss.

That’s all you have to do. The database will happily keep itself updated, though may take a short while to do so. If you add a new file, it might not be in the database immediately. In theory, this lag could mean you miss something – but the odds of that are rather low unless you’re constantly generating additional files.

I guess that means we should talk about using the locate command.

Find Files By Extension With The ‘locate’ Command:

As the title indicates, our goal is to find files by extension. We’ll be using the newly installed and updated locate database. The syntax is really simple. You’d just run a command like this:

For example,  you can find .iso files this way. That command would look like this:

That command will find everything with .iso in the name. If you have a foo.bar.isotext file that’s not really an .iso file, you’ll have to ignore that result because the locate command is going to find it.

But wait, there’s more!

You can limit the search to just a single directory. The command may not look conventional, but the syntax is as follows:

Let’s say that I want to find all the files ending with .iso in my ~/Downloads/ directory. That command would be simple. It’d look like this:

An example output might be something like this:

using the locate command to find files with a certain extension
It’s not hard to use the locate command to find files by extension. You’ve got this!

See? It’s not too hard to use the locate command. You can do more with the locate command, but this is just using it to find files by extension. Read the man page to learn more about it.

EDIT: Closed parentheses thanks to @Osprey.

Closure:

Well, this is a fairly short article. This time around, we just used a different method to find files by extension. There are lots of tools at your disposal and we use the locate command for this one. Just remember to update your database before you use it. Other than that, it’s pretty simple. It’s easy enough for new folks to use it!

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 site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Find Multiple Filenames By Extension – With Locate

In today’s article, we’re going to explore another way to find multiple filenames by extension – with locate. It’s a handy skill to have and will see you installing ‘mlocate’ to get access to the ‘locate’ command. It shouldn’t be a difficult or even very long article.

If this seems really familiar, then you’re paying attention. After all, it was just a couple of days ago that you saw this article:

How To: Find Multiple Filenames By Extension

So, why are we covering the same topic? Well, WordPress, for legitimate security concerns, likes to eat the slash. (It’s a slash and a backslash. There’s no ‘forward slash’ if you want to be *technically* correct.)

Slashes are understood programmatically, by many programs – including PHP. So, in theory it’d be possible to at least probe for exploits with an unescaped slash. The solution is sort of to escape the slash by including two of them, but then WordPress eats that escaping slash every time you save a draft and add to it!

This is extremely frustrating as an author. It seriously sucks. It’s something I’ll need to keep in mind for future articles, always wary of the dastardly slash! At least now I know…

Well, that hassle of escaping the slash also reminded me that we can accomplish the same thing without any slashes, just by using the ‘locate’ command. With the previous article still fresh in my memory, I figured I might as well write the same article – but with a different tool. Why not?!?

Install mlocate:

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.

The ‘locate’ command is actually a part of the mlocate package. It’s not always installed by default, but it should be in every default repository out there. It should be easy enough for you to install. 

For the record, the ‘locate’ command describes itself like:

locate – find files by name

Well, that description looks promising – and is exactly what we’re hoping to accomplish! So then, go ahead and install it. You can install it just like you’d install any other software. In the terminal, it’d look something like:

Install mlocate In RHEL/CentOS:
Install mlocate In Debian/Ubuntu:

That’ll work for most distros, assuming you’re using those package managers. If you’re using a different distro, just go ahead and try the same command but adjusted for your package management software. You should be able to find and install it easily.

NOTE: You’re not done yet. The locate command works off of a database. It’s really quick to generate it and it will use a cron job to keep itself updated after that. So, to get the database started, you’ll want to use this command:

With that done, you’re good to go to the next step…

Find Multiple Filenames By Extension With Locate:

Don’t close your terminal from the previous step! Like oh so many articles, this one also requires an open terminal. So, with your terminal still open, you can start to find filenames by extension with locate. For example:

That will find filenames by extension (with ‘locate’) in the current directory. If you want to specify more filenames, it’s really simple:

You can find just one file by extension:

Or you can find a few files by extension:

The sky’s the proverbial limit and the syntax is so much easier. It’s my understanding that the ‘locate’ command is faster because it relies on a database. I ran a couple of tests, using the article about how to time a command and the results weren’t really conclusive – but I only tested with very simple operations. So, your mileage may vary. Feel free to test it and let me know your results!

Closure:

Well, there’s another article. This time, you’ve learned how to find multiple filenames by extension with ‘locate’, and seen that ‘locate’ is a handy command with easier syntax. So, if you’re interested in the ‘locate’ command, be sure to check the man page (man locate). There are many folks who seem to prefer the ‘locate’ command in general, so it seemed like a good article to include.

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.

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:

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

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:

Or it can be quite targeted:

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:

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:

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

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

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

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.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.