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.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.