SEARCH

How-To Geek

Re-run the Last Command as Root (with Sudo)

Ever forgotten to run the last command with sudo at the beginning? Here’s how to re-run the command with sudo at the beginning, without re-typing the whole thing:

sudo !!

For instance, if you originally did this, but got an error:

rm filename

Then you’d just run the sudo !! command, and it would actually run:

sudo rm filename

That’s all there is to it.

This works because !! is a shortcut way to pull up an item from the bash history. You could use !-1 for the same thing, or !-2 to re-run the command before the last one.

Comments (1)

  1. Larry

    On the other hand, that is typing 7 keystrokes (not counting ENTER), where the more common way (Up-arrow, HOME, sudo and a space is also 7 keystrokes and shows you what its doing before hitting ENTER.

Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 08/2/10

RECOMMENDED POSTS