Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Prevent rm disasters UNIX
The rm command can be quite dangerous, with just a simple slip of the finger (please do not try this at home!):
$ rm -rf php5 *
$ wtf i meant rm -rf php5*
-bash: wtf: command not found
What's just happened there is that the space before the * will result in the permanent removal of everything at and below the currently active directory, including all sub-directories. Ouch. So make use of !$ instead. This feature takes the argument to the last command and applies it to the current command. For instance:

$ ls php5*
// expected 
// list of
// files here

$ rm -rf !$
rm php5*
Now if you make a mistake in the ls command, you'll see the unexpected output before you run the rm command:
$ ls php5 *
// unexpectedly huge
// list of many
// many files here
It will list everything, and you'll notice your mistake before it's too late.
    •    
  • Currently 2.29 / 5
  You rated: 1 / 5 (7 votes cast)
 
[11,851 views]  

Prevent rm disasters | 15 comments | Create New Account
Click here to return to the 'Prevent rm disasters' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Prevent rm disasters
Authored by: htapio on Oct 03, '05 07:39:45AM

I'm using ZShell (/usr/bin/zsh) as my shell instead of bash. After several years I've built a habit of hitting the TAB key to expand the pattern on the command line and I will get what ever my pattern matched before hitting ENTER.

Tapio



[ Reply to This | # ]
zsh again
Authored by: SeanAhern on Oct 03, '05 08:50:12AM

One other advantage of zsh is the automatic detection of the use of * with an rm command. Not only will it prompt you for a double-check, but it can also require an automatic 10 second pause before it does anything, just to give you time to think about it before doing anything catastrophic.

Personally, I never use the system's rm. I alias/function that to code that moves things to ~/.Trash. Much safer! There have been a number of hints here at MacOSXHints about that.



[ Reply to This | # ]
zsh again
Authored by: wgscott on Oct 03, '05 08:58:57AM
I'm also a zsh fan, but wanted to point out that there is a program called wtf that decodes abbreviations (including wtf). Also, here is Gary Kerbaugh's Bourne-again rm:

[ Reply to This | # ]
Not all tokens are passed
Authored by: lullabud on Oct 03, '05 10:37:21AM
Interesting trick. The first thing I see that could be problematic is that it doesn't output ALL tokens from the previous command, but only the last one. Example:
$ echo foo bar cat 
foo bar cat
$ echo !$
echo cat
cat
$
Not a big deal, but I thought it worth noting.

[ Reply to This | # ]
Not all tokens are passed
Authored by: kejadlen on Oct 03, '05 10:45:02AM

!$ gets only the last argument.

!* should get all of the last arguments, though.



[ Reply to This | # ]
Store data under version control software
Authored by: thrig on Oct 03, '05 11:00:50AM

Valuable files should be stored under version control, such as CVS or Subversion. This protects against any manner of accidents by keeping the working files in a sandbox directory. That is, a delete in the sandbox will only affect uncommited changes in that sandbox; the previous versions can be checked out again with a simple 'cvs update' or similar command. Version control also keeps a history of changes with logs, (usually) scales to multiple developers, and more!

My rm points to srm by default, with the horrid interactive option disabled by default.



[ Reply to This | # ]
Prevent rm disasters
Authored by: Anonymous on Oct 03, '05 12:47:32PM
At the risk of being flamed for this, if you say rm -rf * then you get exactly what you asked for. Yes: one slip and it's gone. In my own novice days, I managed an rm -rf .* -- which was fun. (I recommend it on a non-critical system. Great experience fixing it!)

I'd say rm -rf !* is still too easy to get wrong. What if you were thinking of something else and typed it into the wrong shell? What if you missed the bang? What if you were thinking "I will not type space star" and your fingers typed "space star" just to spite you? My own belt-and-braces drunken-safety-mode approach:

$ ls -l php5*
$ ^ls -l^rm -rf
Here, rather than taking the last command's arguments, you're making a wild switcheroo from ls -l to rm -rf -- which amounts to the same thing, but what's the chances of having just typed ls -l as the last thing in that other shell? And here, a stray space or an extra keybounce won't cause untold damage.

[ Reply to This | # ]
Prevent rm disasters
Authored by: Radmin on Oct 03, '05 01:56:18PM

Better that you shouldn't use the command line if you're going to make this sort of mistake... At least after you do this once you'll have such pain associated with rm -rf that you won't be using it again so quickly.

Anyway, if you really need to protect yourself from this sort of stupidity, redefine rm as a shell to the real rm command that will check for this sort of syntax and warn...



[ Reply to This | # ]
Prevent rm disasters
Authored by: codeProle on Oct 03, '05 04:22:40PM

Why not just do this in your .bash_profile:

alias 'rm=rm -i'

Which will force a prompt for each and every file potentially affected by the command.

You can still get into trouble with sudo, but you're own your own there.

---
--
15" G4 TiBook 867MHz 768 MB 40 GB



[ Reply to This | # ]
Prevent rm disasters
Authored by: merlyn on Oct 04, '05 07:11:26AM

Bad idea to alias "rm" itself. Instead, create "del" as "rm -i", and get used to typing "del". The problem is when you type "rm" into a shell that is not aliased like that, you expect "-i" behavior, and it doesn't have it! oops! Big oops!



[ Reply to This | # ]
A Better Solution
Authored by: forman on Oct 03, '05 10:20:06PM

When I was a system administrator, one of the golden rules I learned was to never combine "-rf" and "*" under any circumstance. If one was going to invoke the "-rf" hammer, they should specify the entire path without globs.

Michael.



[ Reply to This | # ]
A Better Solution
Authored by: Anonymous on Oct 04, '05 03:22:25PM

You didn't say how.

1. move files for deletion to /tmp/junk
2. check contents of /tmp/junk
3. check pruned location(s)
4. rm -rf /tmp/junk

Yeah?



[ Reply to This | # ]
Prevent rm disasters
Authored by: macg4dave on Oct 04, '05 10:14:28PM

If you are using the tcsh why not use

"alias rm 'sudo rm -dRfi'" & for folders "alias rmdir 'sudo rm -dfr'"

just pop that in your .tcshrc file and it will ask for a password & show you what you are trashing



[ Reply to This | # ]
A Better Solution
Authored by: macgruder on Oct 05, '05 11:16:59AM

Good point. But that's the whole point of the hint. You bypass putting rm and * together.



[ Reply to This | # ]
Re: -bash: wtf: command not found
Authored by: ChrisAllison on Oct 06, '05 02:52:08AM
$ wtf i meant rm -rf php5*
-bash: wtf: command not found


You obviously don't have wtf installed!

[ Reply to This | # ]