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
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.
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:
alias rm='rm -i'
Next, you’ll save the edited .bashrc file by pressing
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.
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.
Last Updated on September 10, 2023 by KGIII