
(This post is part of a series. To read more tips and to learn about the philosophy behind these posts, check out the parent post.)
You know you’ve done this way too many times:
ben@lispclub ~ $ apt-get install blahblahblah
E: Could not open lock file /var/lib/dpkg/lock – open (13 Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
Bah.
You forgot to type sudo first.
So what do you do? Hit up arrow, swing back to the beginning of the line, type sudo–ARGGH…THE WASTED SECONDS!
Do this instead: sudo !!
In bash, !! means “repeat the last command I entered.” Bash substitutes your apt-get command after the sudo, and you finally get to install blahblahblah 0.0.13. Note that this isn’t specific to sudo. If you do an ls, and then wish you’d grepped the results for foo, type !! | grep foo.
How to remember it:
Imagine an angry father yelling for his son. “Sudo!!” Sudo runs down from his room, sees the command he was supposed to be in front of, and executes it immediately.
14 Comments
Learn bash, man.
Like Ctrl+a
If you suggest an alternative it would helpful if you explained it.
‘sudo’, Space, Bang, Bang: seven keystrokes
Up Arrow, Ctrl-A, ‘sudo’, Space: seven keystrokes
pete: Yes, that’s the same number of keystrokes, but this is a “teach a man to fish” sort of thing, and I think you’re too fixated on this one example. You can use !! in a lot of different scenarios where it’s going to save you more typing.
yea the ctrl-p, ctrl-a, sudo, space is what i usually do, but this is handy.
also, edit your .profile (or similar) and add lines like:
alias apt=’sudo aptitude’
alias svim=’sudo vim’
alias gem=’sudo gem’
and so on. very handy because then you save all 7 keystrokes :)
Thank you!
Here’s a bash function that will either pass its arguments to sudo, or if there are no arguments, pass the previous command to sudo:
function s {
if [ $# -gt 0 ]
then
sudo $@
else
sudo $(fc -ln | tail -n1)
fi
}
How could I avoid entering the password every time?
Nico — this should help: http://sial.org/howto/openssh/publickey-auth/
Timothy — my pleasure!
Alan — Very nice. I might have to write that up later.
Thanks for all the tips. I like that !! think and the cartoon :)
bah… just login as root and be done with it
j/k :p
Yes well, thanks for the tip – One of those things many of us probably “know” but never use.
I’m not sure whether or not the xkcd cartoon would have been improved if it had been used:
Make me a sandwhich.
What? Make it yourself.
Sudo !!
Okay.
Less funny I think.
2 Trackbacks/Pingbacks
[...] However, if you want to prepend the previous command with some other command, say man, watch or that sudo (Codeulate), appending !! still does not save you any keystrokes. However, you still may be [...]
[...] to Leo Laporte’s twitter post of http://codeulate.com/?p=22 I found this wonderful function buried in the comments. [...]
Post a Comment