Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.