Running without backups means you have to be super careful to never make any mistakes. And hope your hardware never fails. (Even RAID can't save you from filesystem corruption caused by faulty RAM.) So that's your first problem. (Which I assume you've already realized and will be doing backups in the future.)
But there are things you can do to reduce the likelihood of mistakes like this:
- alias
rm='rm -I'
to prompt if deleting more than 3 things.
- alias mv and cp to
mv -i
and cp -i
(many normal use-cases for these don't involve overwriting a destination file).
- alias
sudo='sudo '
to do alias expansion on the first argument to sudo
I find rm -I
is a lot more useful than rm -i
. It usually don't prompt during normal use, so getting prompted when you didn't expect it is a lot more noticeable / better warning. With -i
(before I discovered -I
), I got used to typing \rm
to disable alias expansion, after being sure I'd typed the command correctly.
You don't want to get in the habit of relying on rm -i
or -I
aliases to save you. It's your safety line that you hope never gets used. If I actually want to interactively select which matches to delete, or I'm not sure if my glob might match some extra files, I manually type rm -i .../*whatever*
. (Also a good habit in case you're ever in an environment without your aliases).
Defend against fat-fingering Enter by typing ls -d /*foo*
first, then up-arrow and change that to rm -r
after you've finished typing. So the command line never contains rm -rf ~/
or similar dangerous commands at any point. You only "arm" it by changing ls
to rm
with control-a, alt-d to go to the start of the line and adding the -r
or the -f
after you've finished typing the ~/some/sub/dir/
part of the command.
Depending on what you're deleting, actually run the ls -d
first, or not if that wouldn't add anything to what you see with tab-completion. You might start with rm
(without -r
or -rf
) so it's just control-a / control-right (or alt+f) / space / -r
.
(Get used to bash/readline's powerful editing keybindings for moving around quickly, like control-arrows or alt+f/b to move by words, and killing whole words with alt+backspace or alt+d, or control-w. And control-u to kill to the beginning of the line. And control-/ to undo an edit if you go one step too far.
And of course up-arrow history that you can search with control-r / control-s.)
Avoid -rf
unless you actually need it to silence prompts about removing read-only files.
Take extra time to think before pressing return on a sudo
command. Especially if you don't have full backups, or now would be a bad time to have to restore from them.
/
that would be nearly as bad to delete (/etc/
, for example). It simply is not the job ofrm
to decide which directories can or can't easily be deleted. – chepner Feb 12 '19 at 20:30rm
is to delete files and/or directories, not to make judgement calls on which directories are or are not desirable to delete. Sure,/
is an obvious corner case, but if it warns you for/
, should it also warn you for/bin
? For/someRandomDirIAdded
? It's best to keep quiet in all cases, rather than make questionable recommendations in some cases. – chepner Feb 14 '19 at 17:12rm
for doing something you asked it to do. If it always warns you, you quickly stop paying attention to the warnings. If it warns you in some cases, eventually you'll be upset when it deletes something you thought you should have been warned about but weren't. – chepner Feb 14 '19 at 17:15/someRandomDirIAdded
and the entire file system, regardless, my question was already answered so further discussion is unnecessary. – Mister_Fix Feb 15 '19 at 11:35