You might look at the title and think that you’ve seen this before, and you have, but this is another way to find files by extension. That’s right! This is Linux! There’s almost always a variety of ways to think about this. There are almost always a variety of ways to accomplish the same task!
Well, this article will touch on a theme we’ve seen before. We’ve seen it recently. I’ve even explained why you might want to find files by their extension type. Fortunately, I’ve even explained the limitations this has.
See a shell script file could have a .txt extension but still work just fine as a script. Linux cares about the file itself more than it cares about the extension. If you have files mislabeled as an .iso file, this operation will still list them as .iso files. This is only about the extension and using that as a search category. It has limitations.
You can read these articles for more:
Another Way To Locate Files By Extension
Find Multiple Filenames By Extension – With Locate
How To: Find Multiple Filenames By Extension
See? It might seem that this article is like beating a dead horse, but it’s not. There are use cases for all of these options, including the option I’ll give you in this article.
Also, it seems like a good day to take it easy. Life has sent me a bit of extra stress. That won’t stop me from writing this article. It seems nothing will stop me from writing these articles!
This is kind of the sledgehammer way to find files by their extension. It’s a good way. It’s even an easy way to remember, without needing extra commands. At the same time, it’s a pretty basic way to do so.
The first command we’ll work with is one you have installed already. You won’t need to add any software to follow along with this article. We’re simply going to use the ls command to list directory contents.
If you check the man page for the ls command, you’ll see this:
ls – list directory contents
What did I say in the paragraph before that? I said we’re going to list directory contents. Sure enough, Linux provides the perfect tool for this.
As an aside, what you can accomplish with a basic Linux install is amazing. You can accomplish a whole lot of work without actually adding any additional software. This is awesome. Thank you, Linux.
The second command we’ll be using is the grep command. If you’re a regular on this site, you’ll have used the grep command many times. You can think of the grep command as a filter. You use the grep command to process the output of another command (more often than not) and, more specifically, you use it to filter that output.
You want to use grep when you’re trying to isolate some information. Well, sure enough, we can check the man page for grep. If you do so, you’ll find that grep accurately describes itself like this:
grep, egrep, fgrep, rgrep – print lines that match patterns
You’ll see that grep comes in many forms. You’ll also see that it’s used to print lines that match patterns. The command is filtering out those lines that don’t match the prescribed pattern.
In fact, I used it as an example in the previous article:
Find Out When A File Was Created
Scroll down to see how you can use the grep command to filter out the lines that aren’t important when you simply want to find out when a file was created. Simple, eh? Yes… Simple!
As you can guess, we’ll be using both the ‘ls’ command and the ‘grep’ command to find files by extension. As those are terminal-based commands, this will be a terminal-based article. You’ll need to open a terminal and you can (more often than not) do so by pressing CTRL + ALT + T.
With your terminal now open, this is the syntax I want you to use for this exercise. We’ll keep it simple:
ls /path/to/directory | grep ".extension"
If you’ve used mix-case, maybe having extensions of .TXT (in capital letters), you can add the -i flag to the grep command. That will (don’t forget to check the man page) tell the grep command to ignore the case. So, an example of the syntax for that command might be:
ls /path/to/directory | grep -i ".extension"
For example, I might want to search my ~/Downloads directory for .iso files, finding the various distros I’ve downloaded on this computer. If I wanted to do that, my command would look like this:
ls ~/Downloads | grep -i ".iso"
And, sure enough, here’s an example output from that very command:
kgiii@kgiii:~$ ls ~/Downloads | grep -i ".iso" andy_v25a.iso Fedora-Workstation-Live-x86_64-38-1.6.iso MakuluLinux-Max-D-2023-06-30.iso manjaro-mate-22.0-230104-linux61.iso mantic-desktop-amd64.iso mantic-desktop-amd64.iso.zs-old openSUSE-Tumbleweed-NET-x86_64-Snapshot20230821-Media.iso Rocky-9.2-x86_64-dvd.iso
See? That output shows you all the .iso files that I have in my ~/Downloads directory. There’s no fuss. There’s no muss. It’s just a simple way to find files by extension.
Anyone can do it and they won’t need any additional software to do so. Everyone (except maybe someone with a very light embedded Linux system – and probably still them) can find files by extension without adding anything to their system. It’s maybe a bit creative, but it’s effective. I’m not sure about you, but I like effective things. This meets that definition squarely – it’s effective.
Well, I guess this is still a fairly long article. I’ve been making them longer and this one just got written that way. My style of longer writing isn’t without thought. I have altered the format of these articles a half-dozen times.
Each time I do so, I do so for you. The goal of altering the format is to make it easier for you to digest and to offer more information than previous articles – even for the more basic articles like this one. I do not know if I’ll settle on this style. Evidence would suggest that I’ll improve upon it as I go.
I welcome feedback on this. I don’t suppose you’ll give me that feedback. The odds are good that you won’t even read this far down. Still, please let me know if you like the current writing style. I’ve consistently written to a formula, but that formula has changed over time. This is the most recent iteration, so please let me know what you think. Thanks!
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.
Today we'll cover one way to enable or disable your network interface in the Linux…
Today's exercise is a nice and simple exercise where we check your NIC speed in…
Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…
I think I've covered this before with the ls command but this time we'll count…
Today we'll be learning about a basic Linux command that's known as 'uname' and it…
If you've used hardinfo in the past, it may interest you to know that hardinfo…
View Comments
Thanks this is the tutorial I was looking for, many thanks