2

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)

4
  • I assume editing the previous line is not an option? (e.g. Cursor up and edit or ESC k $ a options) ? Commented Sep 25, 2013 at 15:24
  • Usually you're faster by typing rm -rf $_. Commented Sep 25, 2013 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? Commented Sep 25, 2013 at 17:04
  • @jaychris That's what my Usually is meant for. $_ is for the last parameter from the previous command. Commented Sep 25, 2013 at 17:07

1 Answer 1

7

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.

2
  • 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)" Commented Sep 25, 2013 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. Commented Sep 26, 2013 at 9:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.