With so many new users switching or migrating to Linux, it’s becoming not so uncommon to read tales of a noob user deleting a file or files from the command line using the rm command that he/she would like to recover. Usually that happens when the rm command in the user’s distro does not come with a built in safeguard. As a result, rm deletes whatever file is passed to it, without asking for confirmation.

In this short article, I’ll show you how to protect yourself from accidentally deleting files in Linux, in much the same way that most distros protect you from recursively deleting the root directory.

The goal is that any time you call the rm command to delete a file or files, the command will ask for confirmation, giving you the opportunity to change your mind. That is accomplished by adding an alias to the rm command in your distro’s ~/.bashrc file. An alias is a command used to abbreviate a system command or to add default option(s) to a command that you run often.

For the rm command, passing it the -i option will do the magic. (Note that a handful of distros have this out of the box, in which case you don’t have to do anything.)

So open your  ~/.bashrc file with following command:

# open your .bashrc file with your favorite text editor
nano ~/.bashrc

 

Then add the following line to the end of the file or append it to other alias entries in the file, as in the example shown below:

# adding an alias to the rm command
alias rm='rm -i'

 

After adding it, safe and close the file. Then activate the change by closing and reopening the terminal application or by running the following command:

# activating change make to the .bashrc file
source ~/.bashrc

 

After that, the next time you call the rm command to delete a file, it will ask for confirmation like so:

# alias-ed rm command asking for confirmation before deleting a file
rm shared-windows-partition.png
rm: remove regular file 'shared-windows-partition.png'?