After committing the infamous mistake of deleting my entire file system via sudo rm -rf /*
, recovering from the horrendous damage that I had done and coping with the fact that I just lost 6 years off my lifespan, I started wondering why is it even possible to do that, and what could be done to prevent this mistake from happening.
One solution that was suggested to me is revoking root access from my account, but that is inconvenient, because a lot of commands require root access and when you have to run a few dozen commands every day, that gets annoying.
Backing up your system is the obvious way to go. But restoring a backup also requires some downtime, and depending on your system that downtime could be days or weeks, which could be unacceptable in some cases.
My question is: Why not implement a confirmation when the user tries to delete their filesystem? So that when you actually want to do that, you just hit Y or enter, and if you don't at least you don't lose everything.
Meet
safe-rm
, the “wrapper around therm
command to prevent accidental deletions”:If the installation link above doesn’t work for you just use
sudo apt install safe-rm
instead. The default configuration already contains the system directories, let’s tryrm /*
for example:As you see, this would prevent you from deleting
/home
, where I suppose your personal files are stored. However, it does not prevent you from deleting~
or any of its subdirectories if you try deleting them directly. To add the~/precious_photos
directory just add its absolute path with the tilde resolved tosafe-rm
’s config file/etc/safe-rm.conf
, e.g.:For the cases where you run
rm
withoutsudo
1 and the-f
flag it’s a good idea to add analias
for your shell that makesrm
’s-i
flag the default. This wayrm
asks for every file before deleting it:A similarly useful flag is
-I
, just that it only warns “once before removing more than three files, or when removing recursively”, which is “less intrusive than-i
, while still giving protection against most mistakes”:The general danger of these aliases is that you easily get in the habit of relying on them to save you, which may backfire badly when using a different environment.
1:
sudo
ignores aliases, one can work around that by definingalias sudo='sudo '
thoughConfirmation is already there, the problem is the
-f
in the command, that is--force
; When user forces an operation it is supposed they know what they're doing (obviously a mistake could always append).An example:
It is different with
--force
option: I will not get any confirmation and files are deleted.The problem is to know the command and its parameters, navigate more in the
man
of a command (also if the command is found in a tutorial) for examples: the first time I saw the commandtar xzf some.tar.gz
I'm asking myself, "what doesxzf
mean?"Then I read the tar manpage and discovered it.
rm
is a low level system tool. These tools are built as simply as possible as they must be present on any system.rm
is expected to have well known behaviour, especially with regard to confirmation prompts so that it can be used in scripts.Adding a special case to prompt on
rm /*
would not be possible as the rm command doesn't see it in this form. The*
wildcard is expanded by the shell before being passed torm
, so the actual command which needs a special case would be something likerm /bin /boot /dev /etc /home /initrd.img /lib /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var /vmlinuz
. Adding the code to check for this case (which will probably be different on diffferent linuxes) would be a complex challenge as well as being prone to subtle errors. The standard linuxrm
does have a default protection against system destruction by refusing to remove/
without the--no-preserve-root
option.By default there are three protections against deleting your system in this way:
To remove all the contents of a folder, rather than running
rm /path/to/folder/*
, dorm -rf /path/to/folder
, thenmkdir /path/to/folder
as this will trigger the--preserve-root
protection as well as removing any dotfiles in the folderRunning 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:
rm='rm -I'
to prompt if deleting more than 3 things.mv -i
andcp -i
(many normal use-cases for these don't involve overwriting a destination file).sudo='sudo '
to do alias expansion on the first argument tosudo
I find
rm -I
is a lot more useful thanrm -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 typerm -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 torm -r
after you've finished typing. So the command line never containsrm -rf ~/
or similar dangerous commands at any point. You only "arm" it by changingls
torm
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 withrm
(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.Well the short answer is to not run such a command.
The long story is that it's part of the customization. Essentially there are two factors at play here. One is the fact that you are free to modify all files.
The second is that the rm command offers the helpful syntactic sugar to delete all files under a folder.
Effectively this could be restated as a singe simple tenet of Unix machines. Everything is a file. To make matters better, there are access controls, but these are overridden by your usage of
I guess you could add an alias or a function to ensure that this can never be run.
If your system file space usage isn't immense (and these days 'immense' means 'hundreds of gigabytes or more') create some virtual machine instances, and always work inside of one. Recovery would just entail using a backup instance.
Or you could create a chroot jail, and work inside it. You'd still need some recovery if it got trashed, but that would be easier with a running (enclosing) system to work from.
rm
is a very old Unix command and was likely not designed with user-friendliness in mind. It tries to do precisely what it's asked of, when it has the permissions. A pitfall for many new users is that they frequently see code withsudo
and don't think much about using it. Functions that directly modify files likerm
,dd
,chroot
, etc. require extreme care in use.Nowadays I like to use
trash
(without sudo) from trash-cli. It functions like the Recycle Bin from Windows, in that you can easily retrieve accidentally deleted files. Ubuntu already has a Trash folder and move-to-trash functionality built into Files.Even then you may make mistakes so make sure to make backups of your entire filesystem.