I just ran rm -rf /*
accidentally, but I meant rm -rf ./*
(notice the star after the slash).
alias rm='rm -i'
and --preserve-root
by default didn't save me, so are there any automatic safeguards for this?
I wasn't root and cancelled the command immediately, but there were some relaxed permissions somewhere or something because I noticed that my Bash prompt broke already. I don't want to rely on permissions and not being root (I could make the same mistake with sudo
), and I don't want to hunt for mysterious bugs because of one missing file somewhere in the system, so, backups and sudo
are good, but I would like something better for this specific case.
About thinking twice and using the brain. I am using it actually! But I'm using it to solve some complex programming task involving 10 different things. I'm immersed in this task deeply enough, there isn't any brain power left for checking flags and paths, I don't even think in terms of commands and arguments, I think in terms of actions like 'empty current dir', different part of my brain translates them to commands and sometimes it makes mistakes. I want the computer to correct them, at least the dangerous ones.
One of the tricks I follow is to put
#
in the beginning while using therm
command.This prevents accidental execution of
rm
on the wrong file/directory. Once verified, remove#
from the beginning. This trick works, because in Bash a word beginning with#
causes that word and all remaining characters on that line to be ignored. So the command is simply ignored.OR
If you want to prevent any important directory, there is one more trick.
Create a file named
-i
in that directory. How can such a odd file be created? Usingtouch -- -i
ortouch ./-i
Now try
rm -rf *
:Here the
*
will expand-i
to the command line, so your command ultimately becomesrm -rf -i
. Thus command will prompt before removal. You can put this file in your/
,/home/
,/etc/
, etc.OR
Use
--preserve-root
as an option torm
. In therm
included in newercoreutils
packages, this option is the default.OR
Use safe-rm
Excerpt from the web site:
Your problem:
The solution: Don't do that! As a matter of practice, don't use
./
at the beginning of a path. The slashes add no value to the command and will only cause confusion../*
means the same thing as*
, so the above command is better written as:rm -rf *
Here's a related problem. I see the following expression often, where someone assumed that
FOO
is set to something like/home/puppies
. I saw this just today actually, in the documentation from a major software vendor.rm -rf $FOO/
But if
FOO
is not set, this will evaluate torm -rf /
, which will attempt to remove all files on your system. The trailing slash is unnecessary, so as a matter of practice don't use it.The following will do the same thing, and is less likely to corrupt your system:
rm -rf $FOO
I've learned these tips the hard way. When I had my first superuser account 14 years ago, I accidentally ran
rm -rf $FOO/
from within a shell script and destroyed a system. The 4 other sysadmins looked at this and said, 'Yup. Everyone does that once. Now here's your install media (36 floppy disks). Go fix it.'Other people here recommend solutions like
--preserve-root
andsafe-rm
. However, these solutions are not present for all Un*xe-varients and may not work on Solaris, FreeBSD & MacOSX. In addition,safe-rm
requires that you install additional packages on every single Linux system that you use. If you rely onsafe-rm
, what happens when you start a new job and they don't havesafe-rm
installed? These tools are a crutch, and it's much better to rely on known defaults and improve your work habits.Since this is on "Serverfault", I'd like to say this:
If you have dozens or more servers, with a largish team of admins/users, someone is going to
rm -rf
orchown
the wrong directory.You should have a plan for getting the affected service back up with the least possible MTTR.
The best solutions involve changing your habits not to use
rm
directly.One approach is to run
echo rm -rf /stuff/with/wildcards*
first. Check that the output from the wildcards looks reasonable, then use the shell's history to execute the previous command without theecho
.Another approach is to limit the
echo
command to cases where it's blindingly obvious what you'll be deleting. Rather than remove all the files in a directory, remove the directory and create a new one. A good method is to rename the existing directory toDELETE-foo
, then create a new directoryfoo
with appropriate permissions, and finally removeDELETE-foo
. A side benefit of this method is that the command that's entered in your history isrm -rf DELETE-foo
.If you really insist on deleting a bunch of files because you need the directory to remain (because it must always exist, or because you wouldn't have the permission to recreate it), move the files to a different directory, and delete that directory.
(Hit that Alt+. key.)
Deleting a directory from inside would be attractive, because
rm -rf .
is short hence has a low risk of typos. Typical systems don't let you do that, unfortunately. You can torm -rf -- "$PWD"
instead, with a higher risk of typos but most of them lead to removing nothing. Beware that this leaves a dangerous command in your shell history.Whenever you can, use version control. You don't
rm
, youcvs rm
or whatever, and that's undoable.Zsh has options to prompt you before running
rm
with an argument that lists all files in a directory:rm_star_silent
(on by default) prompts before executingrm whatever/*
, andrm_star_wait
(off by default) adds a 10-second delay during which you cannot confirm. This is of limited use if you intended to remove all the files in some directory, because you'll be expecting the prompt already. It can help prevent typos likerm foo *
forrm foo*
.There are many more solutions floating around that involve changing the
rm
command. A limitation of this approach is that one day you'll be on a machine with the realrm
and you'll automatically callrm
, safe in your expectation of a confirmation… and next thing you'll be restoring backups.You could always do an alias, as you mentioned:
You could also integrate it with a commandline twitter client to alert your friends about how you almost humiliated yourself by wiping your hard disk with
rm -fr /*
as root.There's some really bad advice in this thread, luckily most of it has been voted down.
First of all, when you need to be root, become root - sudo and the various alias tricks will make you weak. And worse, they'll make you careless. Learn to do things the right way, stop depending on aliases to protect you. One day you'll get root on a box which doesn't have your training wheels and blow something up.
Second - when you have root, think of yourself as driving a bus full of school children. Sometimes you can rock out to the song on the radio, but other times you need to look both ways, slow things down, and double check all your mirrors.
Third - You hardly ever really have to
rm -rf
- more likely you want tomv something something.bak
ormkdir _trash && mv something _trash/
Fourth - always
ls
your wildcard beforerm
- There's nothing crazy about looking at something before destroying it forever.The simplest way to prevent accidental
rm -rf /*
is to avoid all use of therm
command! In fact, I have always been tempted to runrm /bin/rm
to get rid of the command completely! No, I'm not being facetious.Instead use the
-delete
option of thefind
command, but first before deleting the files I recommend previewing what files you'll be deleting:Note, in modern versions of
find
if you leave out the name of a directory, it will implicitly use the current directory, so the above is the equivalent of:Once you're sure these are the files you want to delete you can then add the
-delete
option:So, not only is
find
safer to use, it is also more expressive, so if you want to delete only certain files in a directory hierarchy that match a particular pattern you could use an expression like this to preview, then delete the files:There are lots of good reasons to learn and use
find
besides just a saferrm
, so you'll thank yourself later if you take the time to learn to usefind
.Yes: Don't work as root and always think twice before acting.
Also, have a look at something like https://launchpad.net/safe-rm.
This is standard of mine specifically for regexps in the context of rm, but it would have saved you in this case.
I always do
echo foo*/[0-9]*{bar,baz}*
first, to see what the regexp is going to match. Once I have the output, I then go back with command-line editing and changeecho
torm -rf
. I never, ever userm -rf
on an untested regexp.The solution to this problem is to take regular backups. Anytime you produce something you don't want to risk losing, back it up. If you find backing up regularly is too painful, then simplify the process so that it's not painful.
For example, if you work on source code, use a tool like
git
to mirror the code and keep history on another machine. If you work on documents, have a script thatrsync
s your documents to another machine.