All Articles

Avoid Using a Powerful "rm"

rm-monika

As we all know, rm is a potent deletion tool in the Linux environment, which can easily lead to the tragic consequence of deleting important files without prompts. I’ve made this mistake myself today, it’s terrifying. I wanted to delete all the files starting with “head”. But somehow I typed rm head * instead of rm head*. All key files were gone unexpectedly without any prompt.

After this incident, I took a lot of time to recover the files, and on the other hand, I took some time to seek some protection tricks to avoid this kind of disaster in the future. We’ve got two methods to avoid this kind of disaster:

  1. force rm to output prompts
$ alias rm = 'rm -i'

Although this can force a check to be performed whenever a file is being deleted, it is a bit too verbose.

  1. move the deleted file to the Recycle Bin
  • create a script file named rm in the ~/.local/bin directory, and add the following content, give the script file executable permission
#!/bin/sh
mv $@ ~/.trash/
  • create the Recycle Bin directory ~/.trash
$ mkdir ~/.trash

The advantage is that all deletions are converted to moving to the Recycle Bin ~/.trash, but some options of rm are not supported by mv.

The current solution is not perfect yet, we need further exploration.

Published Apr 13, 2010

Flying code monkey