Shell Tricks – Volume 2

March 29th, 2009

There is always new cool stuff to learn, especially when it comes to using a command line. It’s always been interesting to me to see scripts and random commands my coworkers use since there is inevitably some arcane yet handy command I never knew existed.

A few months ago I posted Shell Tricks, a pretty random set of short cuts and tricks I use on a regular basis from a shell. Pretty much immediately after finishing it I started this post as a way to keep a running track of things I picked up after the original post. I figured once the list got to a reasonable size I’d clean it up and post it.

  • Change to the last directory – I forget where I picked this up, but I’ve been using it for as long as I can remember. To switch back to the previous directory you were in, run cd -. For example:
  • -> cd code/rhn/spacewalk/
    -> cd ../satellite/
    -> pwd
    /home/jdob/code/rhn/satellite
    -> cd -
    /home/jdob/code/rhn/spacewalk
  • Insert the last argument – While I use the up and down arrows regularly to scroll through previously executed commands, sometimes I need to execute a new command on the same argument I previously used. Typing the escape key and then period will fill in the last argument from the previously executed statement.
  • This is less a shell trick and more of a “if you never knew this existed, it rocks and you want to install it.” My coworker Partha introduced me to rlwrap a few months ago. It came out of a frustration that sqlplus didn’t offer a way to use the up and down arrows to look through a history of SQL I’ve typed. That made it really annoying if I typed out a long ass statement only to find I made a typo early on. Now, if I run rlwrap sqlplus I get all of those nice user input features.

    I’m not sure how clear that is to the reader, so I’ve included a snippet from the rlwrap man page:

    rlwrap runs the specified command, intercepting user input in order
    to provide  readline’s line editing, persistent history and completion.
  • Cowsay – Cowsay is an example of why geeks are pure awesome. When you run cowsay, you give it a string of text and it outputs ASCII art of a cow saying it. I’m not kidding. I know it’s available to both Fedora and Ubuntu distributions, and I’m sure many others offer it. For example:
    -> cowsay Geeks are awesome
     ___________________ 
    < Geeks are awesome >
     ------------------- 
            \   ^__^
             \  (oo)\_______
                (__)\       )\/\
                    ||----w |
                    ||     ||

    In case you were thinking it couldn’t get any more full of win than that, believe it or not it actually supports a pretty detailed series of arguments. For instance, -d renders the cow dead (look at the eyes):

    -> cowsay -d Dead cow
     __________ 
    < Dead cow >
     ---------- 
            \   ^__^
             \  (xx)\_______
                (__)\       )\/\
                 U  ||----w |
                    ||     ||

    There are also options for tired cow, greedy cow, stoned cow (my favorite), and others.

    It gets even better. There are other templates besides cows. For the Canadians:

    -> cowsay -f moose Moose
     _______ 
    < Moose >
     ------- 
      \
       \   \_\_    _/_/
        \      \__/
               (oo)\_______
               (__)\       )\/\
                   ||----w |
                   ||     ||

    A rather detailed dragon v. cow fight:

    -> cowsay -f dragon-and-cow Dragon and Cow
     ________________ 
    < Dragon and Cow >
     ---------------- 
                           \                    ^    /^
                            \                  / \  // \
                             \   |\___/|      /   \//  .\
                              \  /O  O  \__  /    //  | \ \           *----*
                                /     /  \/_/    //   |  \  \          \   |
                                @___@`    \/_   //    |   \   \         \/\ \
                               0/0/|       \/_ //     |    \    \         \  \
                           0/0/0/0/|        \///      |     \     \       |  |
                        0/0/0/0/0/_|_ /   (  //       |      \     _\     |  /
                     0/0/0/0/0/0/`/,_ _ _/  ) ; -.    |    _ _\.-~       /   /
                                 ,-}        _      *-.|.-~-.           .~    ~
                \     \__/        `/\      /                 ~-. _ .-~      /
                 \____(oo)           *.   }            {                   /
                 (    (--)          .----~-.\        \-`                 .~
                 //__\\  \__ Ack!   ///.----..<        \             _ -~
                //    \\               ///-._ _ _ _ _ _ _{^ - - - - ~

    And of course a sheep.

    -> cowsay -f sheep Sheep
     _______ 
    < Sheep >
     ------- 
      \
       \
           __     
          UooU\.'@@@@@@`.
          \__/(@@@@@@@@@@)
               (@@@@@@@@)
               `YY~~~~YY'
                ||    ||

    I’ll stop there. Trust me, there are more. This is what happens when you let geeks have free time.

  • The sl command is arguably even cooler than cowsay. I can’t even fully do it justice in a blog entry, so I’ll leave it to the reader to install and see for yourself. My understanding is that it was meant as a way to mess with noobs who typed “sl” instead of “ls”. Not sure how legit that is, but I like the explanation. :)

Comments are closed.