Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to repeat your last command with additional flags?

Example:

Say I do this:

rm myFavouriteDirectory

I will get a reply:

rm: myFavouriteDirectory/: is a directory

Obviously what I meant to do was:

rm -rf myFavouriteDirectory

Is there some way to repeat the last command with flags like !! -rf (this doesn't work)

share|improve this question
    
I assume editing the previous line is not an option? (e.g. Cursor up and edit or ESC k $ a options) ? – Hennes Sep 25 '13 at 15:24
    
Usually you're faster by typing rm -rf $_. – ott-- Sep 25 '13 at 15:34
    
@ott: What if the command is a long one? Also $_ stands only for the last parameter? similar to !$? What if there are multiple parameters? – jaychris Sep 25 '13 at 17:04
    
@jaychris That's what my Usually is meant for. $_ is for the last parameter from the previous command. – ott-- Sep 25 '13 at 17:07

See "HISTORY EXPANSION" in man bash.

The closest I'm aware of would be:
!!:0 -rf !*

!!:0 is the first word in the previous command, !* is everything but the first (i.e. the arguments).

But in practice I would always type:
rm -rf !*

In GNU distributions, !! -rf (i.e. putting the flags at the end) would still work for most commands, but it usually won't on BSD and OSX. It depends how the command processes arguments.

share|improve this answer
1  
In my bash, !! -rf works (simpler than !!:0 !*). I don't know if this is dependent on the bash version (as you have mentioned about GNU distribution). Mine is "GNU bash, version 3.00.15(1)" – jaychris Sep 25 '13 at 17:06
    
How the tool (rm in this case) processes arguments is more what I was referring to. GNU utils on Linux generally accept option flags at the end after other arguments, whereas BSD (and OSX) do not. But you make a good point, !!:0 !* -rf is completely pointless and no better than !! -rf, I'll update my answer. – Alex Forbes Sep 26 '13 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .