Command Line

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:
sudo yum install mlocate
Install mlocate In Debian/Ubuntu:
sudo apt install mlocate

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:

sudo updatedb

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:

locate -i "*.iso" "*.deb"

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

locate -i ~/Downloads "*.iso" "*.deb"

You can find just one file by extension:

locate -i "*.iso"

Or you can find a few files by extension:

locate -i "*.iso" "*.deb" "*.zip

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.

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.

Recent Posts

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

19 hours ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

3 days ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

5 days ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

7 days ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

1 week ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

2 weeks ago