Yet Another Way To Find Files By Extension

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!

The ‘ls’ Command:

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 ‘grep’ Command:

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!

Find Files By Extension:

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:

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:

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:

And, sure enough, here’s an example output from that very command:

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.

Closure:

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.

Send A Message To Logged In Users

Today’s article will have limited use for my regular readers, as we talk about how you can send a message to logged-in users. The thing is, this is only valid for those users who are logged into the terminal. It doesn’t pop up a handy GUI window (like the old messenger service from Windows, though I’m sure there’s a way to do so). This is only valid for users who have logged into the terminal.

Got it?

Good!

So, who is this useful for? Well, those of you who have shell users. This is useful for system admins who want to send mass messages to the people who are currently logged in. For example, maybe you want to message folks to let them know that you’ll be doing routine maintenance and rebooting the system at a specific time.

While this is a bit archaic, it’s still useful under some circumstances.

Why do I include it if it’s so archaic and has limited use?

Well, because I can. You never know when someone doesn’t know something and will hit up their favorite search engine to learn something. I care that each article teaches you something, even if that something isn’t all that grandiose. 

Plus, it’s nice to have an easy article now and then. This is going to be a pretty easy article! There are just a couple of tips that I have for folks and that’s the end of it.

Send A Message To Logged In Users:

You’ll need an open terminal if the opening wasn’t descriptive enough. After all, we’re sending messages to users who are logged in with the terminal. So, open said terminal. Most often, you can press CTRL + ALT + T and your default terminal should open.

With your terminal open, you can be reasonably sure that the wall command is available. Run this command to be certain that the wall command is available:

You can then check the man page to ensure that this is the correct tool for the job. As you’ll see, if you run man wall, this is the tool for the job:

wall – write a message to all users

See? It is the right tool for the job. The syntax is even remarkably simple:

Or, if you do this often and want consistency:

So, you can try something like:

Or, if you do this often, you can make a .txt file with your message and just reference that file in the wall command. That’d look like the above example.

See? That’s it. That’s all you need to know to send a message to logged in users. If you’re new to your admin job or maybe have started running a public-access shell, you’re now able to send those messages to your users.

Closure:

I figured it was a good day for a nice easy article. They can’t all be complicated, because not everything is complicated. Sometimes, it doesn’t take 1000+ words to describe a task. I could probably bloviate and digress, but that’d just make the article longer with no real value.

As much as it might seem otherwise, I value your time. I try to remain on topic and I try to include no more than the information you need – with just a bit of digression in the intro. I figure most of you skip the intro anyhow!

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.

Compare Two Text Files Side By Side

Today’s article has a theme that may seem familiar, as I recently wrote an article about how to compare two text files side by side. That article was well received and I figured I’d share another way to compare two text files side by side. After all, this is Linux and there are usually many paths to the same destination.

You can use a variety of tools to compare files. It’s not hard to check two files to see the differences between them. However, you can visually compare them quite easily. All you need to do is to show those two files in a side-by-side manner.

The previous article can be read here:

Show Two Text Files Side By Side

So, that’s one way to show two text files side by side. This article is going to show you another way. Why? Because I can! Also, I’d like to write this article while it’s still fresh in my memory. If I don’t write it soon, I’ll have forgotten to write it at all. Age will do that to a person. It is what it is.

Preparation:

In the previous article, I had you download two files. I’m going to suggest that you do so again. That way, we’re on the same page. 

Download these two files to your ~/Documents directory:

File One
File Two

Those two files should be the same in every way. Byte for byte, they should be the same (with maybe a slight difference in metadata like last accessed time).

The tool we’ll be using is sdiff and you almost certainly have this installed by default. It’s part of the diff tools, so it is likely installed by default. You can verify that sdiff is previously installed with the following command:

 The output should show you that sdiff is just a part of the diff utilities. It should say something similar to this:

sdiff (GNU diffutils) 3.8

Those are the only tools you’ll need. Well, that and a terminal emulator of your choosing. It’ll be easy enough for anyone to follow along with this article.

Compare Two Text Files Side By Side:

As I mentioned above, you’ll need an open terminal. For most distros, you can just press CTRL + ALT + T and your default terminal emulator should open. If not, open a terminal from your application menu.

If you followed the above directions and downloaded the two files, you will first need to change your directory to your ~/Documents directory. That’s easily done with this command:

Then, you can simply compare the two files. The syntax is like this:

So, in this case, the command would look like this:

As you can see the files are the same.

using sdiff to compare two text files side by side
See? The files are the same. You can use sdiff to compare two text files side by side.

Let’s mix things up…

Change One File:

We already have the terminal open, so let’s see this sdiff in action with a changed file. Let’s edit the sort1.txt file with Nano. Run this command:

Now, scroll to the bottom and add the following line:

Then, you need to save the updated sort1.txt file. As we’re using Nano, you can do that by pressing CTRL + X, then Y, and then ENTER. It’s not hard to save a file in Nano.

Now, let’s run the sdiff command a second time so that we can compare two different text files side by side. 

You’ll see a different output. It should look like this:

comparing two different files with sdiff
You can see that one line is different when you compare the two text files.

The > character indicates that there’s a difference. Specifically, it indicates that there’s a new line that isn’t in the first text file. 

On the other hand…

comparing two files with sdiff and one has a different line
This time, the sdiff command is showing two different text files side by side.

The | character shows that the lines are different. This is a different character that shows that there’s a new line. Instead, this one is indicating that the text on that line is different than the file it is being compared with.

See? Pretty simple stuff!

Closure:

Well, this is another way to compare two text files side by side in the Linux terminal. There are so many ways to accomplish a goal in Linux and this is just another way that you can do this operation on text files. This sort of stuff would be handy in dealing with revisions or easily spotting differences in readme files. You can apply this skill to all sorts of situations. Feel free to comment telling me how you use the sdiff command.

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.

Another Way To Locate Files By Extension

I figure that today is a good day for a shorter article, so we’ll look at another way to locate files by extension. This is something that you can do in so many different ways. We’ll just cover another one, simply because we can.

You might want to locate files by extension when you’re doing file management tasks. When you have a directory containing many files, the output from the ls command can be pretty cluttered. For example, you might want only to list the files with a .txt extension. This article will help with that.

It should be noted that Linux doesn’t much care about file extensions. If you create a plain text file without any extension at all and add some text to the file, Linux will know that it’s a text file. See this article on Wikipedia about magic bytes:

List of file signatures

See also this article:

Magic Number on Wikipedia

About a year ago, I wrote a couple of articles on this same subject – that is finding files by extension. Of course, those articles are different than this article. This is a different way to locate files by extension. Those previous articles were:

Find Multiple Filenames By Extension – With Locate
How To: Find Multiple Filenames By Extension

So, finding files by their extension type has been covered before. We’re just going about it another way. The previous examples were a bit convoluted and this is just going to be an “easier” way. (I suspect that it’ll be easier. I guess that depends on what you consider to be easy!)

Locate Files By Extension:

Yes, we’ll be doing this in the terminal. I’m sure there’s a GUI way to do so, but that’s going to be a matter of the file manager you use. They will not all have the same features and not all of them will access those features in the same way. So, let’s get started by opening your terminal – just press CTRL + ALT + T and your default terminal should open.

With your terminal now open, we’ll use the find command again. You almost certainly won’t need to install anything but you can confirm that you have find installed with this command:

See? You have the find application installed already. 

If you check the man page (with man find), you’ll see find is described as:

find – search for files in a directory hierarchy

We’ll also be using grep and a pipe “ |” in this article. You should know that grep helps you sort stuff and that the pipe lets you take the output from one command and use it in another. The pipe is a very underrated tool by people who don’t know much about the terminal.

The command we’ll be using is a find command. The syntax is quite simple and would look something like this:

For example:

The -type f is telling the find command to only look for regular files. Then, the output from that command is then piped to the grep command. After that, the grep command looks for file names that contain a . and the specific extension you’re looking for. The output might look like this:

using find and grep to locate files by extension
This is a fairly new installation and most of my .iso files are stored on the network.

Now, this isn’t going to work if you just use wonky file names. This isn’t going to find files without that extension name. Remember, we’re trying to locate files by extension. We are not determining if the extension is correct and we are not determining what the file really is.

See? Pretty simple!

Closure:

Well, I said today’s article would be shorter. It’s also fairly simple. If you need to locate files by extension, there are many ways to do it. Some methods are easier than others. Perhaps some are more robust than other methods. Either way, you can now locate files by extension type. 

You might also be interested in this article:

Find Out What A File Is

Otherwise…

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.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.