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.

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.

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.

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.

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.

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...

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!

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.