RSS
 

Damn Useful: When You Forget to type Sudo

09 Dec

(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.

 
16 Comments

Posted in coding

 
  • oh dang

    Learn bash, man.
    Like Ctrl+a

  • Horton

    If you suggest an alternative it would helpful if you explained it.

  • pete

    ‘sudo’, Space, Bang, Bang: seven keystrokes
    Up Arrow, Ctrl-A, ‘sudo’, Space: seven keystrokes

  • Ben Orenstein

    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.

  • dude

    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 :)

  • Timothy Andrew

    Thank you!

  • http://alan.dipert.org Alan

    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
    }

  • Nico

    How could I avoid entering the password every time?

  • Ben Orenstein
  • Ben Orenstein

    Timothy — my pleasure!

  • Ben Orenstein

    Alan — Very nice. I might have to write that up later.

  • http://ecomputicus.blogspot.com Marco

    Thanks for all the tips. I like that !! think and the cartoon :)

  • http://dagobart.wordpress.com/2008/12/09/another-useful-bash-skill/ another useful Bash skill « Arnold Funken on the Social Web

    [...] 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 [...]

  • http://2blog.kreme.com/2008/272-bash-function-ftw Brain Drippings » Blog Archive » bash function, FTW

    [...] to Leo Laporte’s twitter post of http://codeulate.com/?p=22 I found this wonderful function buried in the comments. [...]

  • Bootdisk

    bah… just login as root and be done with it

    j/k :p

  • http://justin-hopkins.com Justin

    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.