Remove Files By Extension

This should be a quick and easy article, easy enough for anyone to follow, as it covers how you can remove files by extension in Linux. There are a few tips and tricks you can use for this and we’ll be managing files in the Linux terminal. Read on to learn more!

As an aside, I’m thinking about doing some ‘shorts’, which would be short articles that cover a simple topic and do so concisely. If you have any thoughts on this, feel free to opine in a comment.

While Linux doesn’t necessarily care about file extensions, you’ll still often have file extensions. They’re useful for the user, as a way to visually determine a file’s usage quickly. If you see a .sh file, you can guess that it’s a script. When you see a .deb file, you can be reasonably sure it’s a package. You can assume a .png file is an image file.

Today, we’ll be using the ‘rm’ command and wildcards. We’ll be learning how to remove files by extension in the terminal. So, be prepared for that.

The ‘rm’ Command:

You won’t need to install anything for this article. You certainly won’t need to install the rm package. The rm package is a part of the core utilities. You can confirm that rm is available by entering rm --version in the terminal. (I do wish that was consistent, but it is not.)

If you don’t know, you can check the man page, to see the rm command is described like so:

rm – remove files or directories

As the goal is to remove files by extension, this seems like it’d be a good tool for the job at hand. Sure enough, it is!

Remove Files By Extension In Linux:

If you read the intro, and so few of you do, you’d know that we’re going to remove files by extension in Linux – with the terminal. So, open a terminal. More often than not, you can just press CTRL + ALT + T and your default terminal should open.

With your terminal open, the syntax is as follows:

Let’s use .deb as an example. If you want to remove all the .deb files in the folder you’re in, try this command:

The wildcard (the asterisk) means any characters can be there. You’ll remove foo.deb and bar.deb with the above command.

While that’s all there is to it, you can use wildcards for other things.

You can use a wildcard to match other patterns. If you had fle_1_foo and file_2_foo, you could remove them with this command:

If you had file_foo_1 and file_foo_2, you could remove them all with this command:

Pretty sweet, huh?

There’s more to the rm command. If you wanted to do this with folders, you’ll find that rm doesn’t do that by default. So, just add the -r flag to your command, like so:

If you’re having issues removing something with the rm command, there’s a flag you can use to force it. That flag is the -f flag and it’s used something like this:

I guess this article is more about the wildcard than it is about learning how to remove a file by extension. That’s just one way to introduce people to the concept, now that I look at it this way.

The wildcard is a pretty powerful tool in Linux, a tool that’s very useful in the terminal and while doing advanced file management. Knowing how (and when) to use a wildcard will do you well. If you have any questions, you can always ask and I’ll see if I have an answer.

Closure:

So, we’ve covered how to use a wildcard to remove a file by extension. You can do this with folders and other files, not just by extension. What you’d be doing would be pattern recognition and Linux is more than happy to help you along the way. It’s a pretty powerful tool and a tool that every Linux user should be familiar with.

Hmm… I guess 700+ words is short these days. Ah well…

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.

Let’s Make The rm Command Even Safer

Today’s article is one I don’t expect most folks to follow, but it’ll be a fun way to make the rm command even safer. If this article sounds familiar, then you remember a recent article. Well, this article takes it a step further and we’re making the rm command even safer!

See, in the last article you learned how to:

Let’s Make The rm Command A Little Safer

Despite the naysayers not seeing the value, that article will help you make the rm command a little safer. It adds a sanity check. If you followed the directions, you’d get the chance to tell the rm command to ‘never mind’ and to ignore your command prompt. This can stop you from removing files accidentally.

While I wrote that article, I already had this one planned, but didn’t want to publish the two back-to-back. I try to mix things up a little.

NOTE: If you followed the directions in the first article and decide you’d rather go this route, you’ll have to undo the actions you took. These two things can not work together at the same time. It’s a one or the other kind of thing.

What will we be doing in this article?

Well, simply put, we’re going to use an alias like we did in the previous article about making the rm command safer – except we’re also going to define a function and alias the rm command to that function instead.

What will that function do? Well, in short, it will take the results of the rm command and stick the files in your trash (recycle) bin. It makes the rm command work similarly to your regular delete (depending on the distro). Instead of deleting files, it happily sends them to the trash bin instead. So, if you screw up you can restore the files nice and easily with your GUI (by going into the trash bin and restoring the files, of course).

This is a bit more complicated. That’s why I went with the previous rm command modifications earlier. This still isn’t all that complicated. I realize this is something new to most of my readers, so I’ll make it as clear as humanly possible. (Wish me luck!)

In the previous article, we made the rm command a little safer. This time around, we’re going to try to …

Make The rm Command Even Safer:

Yes, you’ll need an open terminal. You can likely press CTRL + ALT + T and your default terminal should open. Otherwise, open it from your application menu.

These directions will assume you’re using Nano. If you are not using Nano, please adjust the directions to suit your text editor of choice.

With your terminal open, I want you to enter the following command:

Then, and this may be confusing, I want you to copy and paste all of the following text at the very bottom of that file:

Next, you need to save the file. As we’re using Nano, you save the file by pressing CTRL + X, then Y, and then ENTER.

Next, you need to tell your system to reload the ~/.bashrc file. You can log out and back in, you can close all the terminals and open a new one, or you can simply type the following:

NOTE: In the second code block you’ll see ~/.local/share/Trash/files. This should be the correct path to your trash bin. You should verify this and change the path accordingly. This directory ONLY exists if you’ve previously moved something to the trash. You’ll need to create a file and move it to the trash or you’ll have to make the directory manually.

Testing This New rm Command:

Leave your terminal open and open your GUI file editor. With your GUI file editor open, navigate to ~/.local/share/Trash/files.  You may not see it by default, so change your GUI file manager’s options to show hidden files and directories. (You can often use CTRL + H to show hidden files.)

Now that you’ve navigated to the directory with your GUI file manager, return to your terminal emulator. Once there, type the following:

You can then run ls to ensure the file foo.txt exists. Next, you’re going to delete it with the rm command:

Again, you can then run ls to ensure the foo.txt file has disappeared. It should certainly be gone from the directory you were in and removed by the rm command.

Except, it wasn’t!

Go back to your GUI file manager and (you might need to refresh it, depending on the file manager) check the list of files. Sure enough, you should see a foo.txt in the trash bin. It’ll remain there until you restore it or until you empty the trash.

Pretty neat, huh? 

Closure:

The first article made the rm command safer. This article will help you make the rm command even safer! You can’t do both (easily), but you can do one or the other. I mean, I’m sure it’s possible but I’ve never thought of a way to do so. I haven’t tried to think of a way to do so. So, there’s that…

If you follow this article, your rm command will send the files deleted by rm to the trash bin. If you follow the first article, you get the chance to view your command’s outcome and decide to back out of the file removal process. You can pick whichever one works best for you.

You can also completely ignore these things and just keep on doing what you have been doing. This being Linux, you can make all sorts of choices – including this one. Though, I think it was Rush who said, “If you choose not to decide you still have made a choice.”

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.

Let’s Make The rm Command A Little Safer

In today’s article, we will discuss a simple trick that will help you make the rm command a little safer. This isn’t something everyone will do, but it might be something a cautious user might do. It might also be something a new user might want to consider doing. After all, you can’t be too safe!

Now, when I say safe, I mean preventing you from accidentally removing files you don’t want to delete. I do not mean safe as in security. I mean safe as in preserving data that is important to you. This isn’t a foolproof method, but it gives you a chance to rethink things.

Obviously, we’ll be using the rm command. Don’t worry, that’s not something you’re going to need to install. It’ll be there with your distro as a part of the standard tools, probably from the nice GNU folks.

For those that don’t know, the rm command is a terminal-based command used to remove files and directories. It does exactly what you tell it to do and sometimes you may tell it to do things you don’t want it to do. Either way, the man page (man rm) describes the rm command succinctly:

rm – remove files or directories

See? Nice and simple.

We’ll also be adding an alias. I intend to write a good article about aliasing things, but this is not that article. Don’t worry, you won’t need to know a whole lot about aliases in order to follow along with this article. I’ll make it nice and simple.

Make The rm Command A Little Safer:

As mentioned in the preamble, the rm command is used in the terminal. So, of course, you’ll need an open terminal for this one. It won’t be too complicated, so press CTRL + ALT + T and your default terminal should open. If it doesn’t, find it in your software menu and open it that way.

NOTE: This article relates to the removal of files over which you the user have control, eg ~/home/<your-username>. If you’re removing files with sudo, root, or as another user, the rm command will function as normal.

Alright, so what’s an alias? An alias is a substitution. You can create an alias that inputs ‘foo’ even though you typed ‘bar’, and that’s the trick we’ll be using to make the rm command a little safer for you. If you don’t do this and you remove important files, all I can say is I told you so! 

I’m going to assume that you have Nano installed. If you do not have Nano installed, you can use a different text editor. For simplicity’s sake, we’ll just go over doing this with Nano.

Let’s Install Nano (With Some Bonus Information)

With Nano installed, your first command is going to be opening up your ~/.bashrc file for editing. You do that with this command:

Yes, the . means that it is a hidden file, but Nano finds it just fine.

Now, you want to use the arrow keys to scroll to the absolute bottom of that file and add the following line:

Next, you’ll save the edited .bashrc file by pressing CTRL + X, then Y, and then ENTER.

With that done, you’ll need to tell the system to reload the .bashrc file. You do that with this command:

Now, when you use the rm command, it will add the -i flag. This will show you the files that the rm command is going to delete and give you a chance to back out of it.

Closure:

See that? You’ve now managed to make the rm command a little safer and you’ve added an alias to the rm command. Pretty neat, I think. This means that you’ve got that second chance when you misfire an rm command. It also means you get to really think about it and that the process doesn’t run without your giving it confirmation to do so.

One of these days I’ll figure out how to write an article about aliases. They’re really handy things to have around and you can use them for all sorts of neat things. I suppose you could just search for that information. Other people have written articles on the subject, probably better than I will. So, there’s always that option… Which is nice…

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.