1

I accidentally removed a bunch of files I wasn't supposed to. I would like to implement some kind of safety net in the future to protect my files.

I was hoping that any time I type rm * into the terminal it would ask me if I'm sure I want to delete all files. Any tips?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

4 Answers4

3

Typing

rm *

should, by itself, set off alarm bells. This is a perfectly legal command to want to execute from time to time, but before doing so, you should check that you're in the right directory (maybe make the current directory part of your prompt?), that * will match the filenames of the files that you want to remove (you can check this with echo *), etc. etc.

There is an -i option to rm that you could use, which would ask for confirmation before removing a file. Creating an alias for rm to rm -i has over time proven that people will start to rely on this option to catch stupid mistakes, which would lead to nasty surprises in situations where the alias happens to not be set. It would therefore be better to develop a habit of thinking before pressing Enter, especially when running things that deletes files (or overwrite files, or truncate files).

Either that, or (and!) keep readily available hourly backups of everything that you don't ever want to loose. I recommend restic or borgbackup for off-site backups.

Kusalananda
  • 333,661
2

Best practice, and what I've been doing for years, is to activate autocompletion in my shell and just pressing tab (or whatever key you map this function to) so you can see the files that * will expand to once you press the key.

Also, I added an rm alias like this:

alias rm='nocorrect rm -Iv'
  • nocorrect basically takes care of ignoring the sometimes redundant autocorrection message when typing rm in my shell, Zsh. Let's say you have a file called rmi in your current directory, or there is a binary called rmi, nocorrect will take care of this.

  • -I is for a prompt when rm is deleting more than 3 files or when removing recursively, ie. directories.

  • -v for verbose, is just to see what rm is deleting; I might just want to cancel this operation (Ctrl-c) if for some reason I'm deleting the wrong files. Rarely used but nice to see the process.
2

If using the tcsh shell, you can set the rmstar variable for that:

tcsh> set rmstar
tcsh> touch file
tcsh> rm *
Do you really want to delete all files? [N/y]

That however doesn't apply to

tcsh> rm ./*
tcsh>

Too bad, as rm ./* is really what you should be doing. rm * is unsafe because it doesn't work if there are files whose name starts with -.

zsh adapted and improved that tcsh feature and made it the default:

zsh$ rm ./*
zsh: sure you want to delete the only file in /home/chazelas/. [yn]?
zsh$ rm foo /*
zsh: sure you want to delete all 33 files in / [yn]?

(note that it happens before rm is even involved. Above foo is not deleted until you press y).

You can disable it with

set -o rmstarsilent

There's even a rmstarwait option for the trigger-happy users:

RM_STAR_WAIT:

If querying the user before executing 'rm ' or 'rm path/', first wait ten seconds and ignore anything typed in that time. This avoids the problem of reflexively answering 'yes' to the query when one didn't really mean it. The wait and query can always be avoided by expanding the '*' in ZLE (with tab).

Another way to bypass that check is to use rm ./**.

1

Get in the habit of using 'rm -i' instead. Avoid using an open * unless you have to.

kevlinux
  • 389