Quantcast
Channel: Admins Goodies » rm
Viewing all articles
Browse latest Browse all 10

How do I prevent accidental rm -rf /*?

$
0
0

Question

Just run rm -rf /* accidentally, 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?


Yeah, I wasn’t root and canceled 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 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 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 computer to correct them, at least dangerous ones.


Thanks for the answers I’ll try them and accept one when my box finishes restoring from backup tomorrow!

Answer

One of the trick I follow is to put # in the beginning while using rm command.

root@localhost:~# #rm -rf /

This prevents accidental execution of rm on 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 such a odd file can be created?

Using touch -- -i or touch ./-i

Now try rm -rf *.

sachin@sachin-ThinkPad-T420:~$ touch {1..4}
sachin@sachin-ThinkPad-T420:~$ touch -- -i
sachin@sachin-ThinkPad-T420:~$ ls
1  2  3  4  -i
sachin@sachin-ThinkPad-T420:~$ rm -rf *
rm: remove regular empty file `1'? n
rm: remove regular empty file `2'? 

Here the * will expand -i to the command line, so your command ultimately becomes rm -rf -i. Thus command will prompt before removal. You can put this file in your /, /home/ and /etc/ etc.

OR

Use --preseve-root as an option to rm. In rm included in newer coreutils package, this option is default.

--preserve-root
              do not remove `/' (default)

OR

Use safe-rm

Excerpt from the web site:

Safe-rm is a safety tool intended to prevent the accidental deletion
of important files by replacing /bin/rm with a wrapper, which checks
the given arguments against a configurable blacklist of files and
directories that should never be removed.

Users who attempt to delete one of these protected files or
directories will not be able to do so and will be shown a warning
message instead:

$ rm -rf /usr
Skipping /usr

Viewing all articles
Browse latest Browse all 10

Trending Articles