Zero Wind – Jamie Wong Inside the mind of a Waterloo Software Engineering student

29Jun/102

Commandline Tips and Tricks

Alas, I have been stricken with the plague of half-finished posts and projects. But, as I feel the need to have at least one post per month, here's some stuff you might find useful.

I forget where I discovered this, but commandlinefu.com is an awesome site.

It's so awesome in fact, that I think the productivity gains I've gotten from it almost outweigh the time I wasted looking through the site and testing it.

Here are some of the more useful ones from the site and a few I learned at work and elsewhere.

In all the examples below, the stuff inside [ ]'s is my current working directory. It's the result of my .bash_profile command

export PS1="[\e]2;Jamie@Local \a\e[32;1m] 
\033[1;32m  [\W]  \033[1;34;40m\033[1;32;0m"

And yes, I have been spending a lot of time on Stack Overflow.

Change to previous working directory

From: Change to the previous working directory @ commandlinefu.com

  [stackoverflow]  pwd
/Users/jamiewong/code/stackoverflow

[stackoverflow] cd /usr/bin

[bin] pwd /usr/bin

[bin] cd - /Users/jamiewong/code/stackoverflow

[stackoverflow] pwd /Users/jamiewong/code/stackoverflow

Rerun the last command

From: Run the previous command with sudo @ commandlinefu.com

  [stackoverflow]  echo hello
hello

[stackoverflow] !! echo hello hello

  [stackoverflow]  ln -s some_script.sh /usr/bin/some_script
ln: /usr/bin/some_script: Permission denied

[stackoverflow] sudo !! sudo ln -s some_script.sh /usr/bin/some_script

[stackoverflow] ls -l /usr/bin/some_script lrwxr-xr-x 1 root wheel 14 30 Jun 00:44 /usr/bin/some_script -> some_script.sh

  [stackoverflow]  cat /etc/apache2/passenger_pane_vhosts/searchgraph* 
<VirtualHost :80>
  ServerName searchgraph.local
  DocumentRoot "/Users/jamiewong/Code/searchgraph/public"
  RailsEnv development
  <Directory "/Users/jamiewong/Code/searchgraph/public">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost> 
  [stackoverflow]  echo "!!"
echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph "
cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf

Modify the last command and run it

From: Runs the previous command but replacing @ commandlinefu.com

  [stackoverflow]  echo hello world
hello world

[stackoverflow] ^world^friends echo hello friends hello friends

  [stackoverflow]  ls *.php
bolding.php shd.php     stream.php

[stackoverflow] ^php^rb ls *.rb hms.rb split_orderby.rb

  [stackoverflow]  cds ..
-bash: cds: command not found

[stackoverflow] ^s cd ..

[code]

List your last n commands

  [stackoverflow]  history 10
  522  locate mysqld
  523  sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
  524  sudo /Library/PreferencePanes/MySQL.prefPane/Contents/MacOS/MySQL
  525  sudo mysqld_safe
  526  sudo /usr/local/mysql/bin/mysqld_safe
  527  mysql
  528  history
  529  history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
  530  history
  531  history 10

Edit and run a previous command

From: Edit the last command line in an editor then execute @ commandlinefu.com This one requires a bit of explanation. The fc command will, by default, open your last command in the console editor you specify (emacs by default - yuck). If you want it to be vim (yay!), stick this in your .bash_profile to just run it when you want to use it.

export EDITOR=vim
Then run fc, and it will open up your editor with your last command in it. Save and exit (:wq) to run that command. If you just want to execute it immediately, use fc -s.
  [stackoverflow]  history 5
   86  cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf 
   87  echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf "
   88  history 10
   89  echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf "
   90  history 5

[stackoverflow] fc -s 87 echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf " cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf

If you don't care to look up the history number but happen to remember that it was the command you executed two lines ago, you can use negative history numbers
  [stackoverflow]  echo hello
hello

[stackoverflow] echo world world

[stackoverflow] fc -s -2 echo hello hello

Follow new output to a log

This one comes from my coworkers

  [stackoverflow]  tail -f ~/code/searchgraph/log/development.log

Processing HomeController#bp (for 127.0.0.1 at 2010-06-23 22:19:27) [GET] Rendering home/bp Completed in 6ms (View: 5, DB: 0) | 200 OK [http://searchgraph.local/bp]

Processing HomeController#bp (for 127.0.0.1 at 2010-06-23 22:20:46) [GET] Rendering home/bp Completed in 9ms (View: 7, DB: 0) | 200 OK [http://searchgraph.local/bp]

Then if I go open up searchgraph.local, my console window will get updated on its own

Copying and pasting from files

The pbcopy and pbpaste commands copy and paste from your clipboard.

  [stackoverflow]  echo test > test.txt

[stackoverflow] pbcopy < test.txt

[stackoverflow] pbpaste test

And if you want to paste content you have in your clipboard to a file, just redirect, with
pbpaste > output.txt

That's all for now - I'll write more down as I find them.

Comments (2) Trackbacks (0)
  1. Here are some I like:

    Bash brace expansion, and multiple use cases:

    $ echo {this,is,brace,expansion,{10..1},blast,off} this is brace expansion 10 9 8 7 6 5 4 3 2 1 blast off

    $ ls source.cc $ cp source.cc{,.bak} $ ls source.cc source.cc.bak

    $ mkdir -p a/b/c/d/e/{asdf,ajkf,{1..5}} $ find a a a/b a/b/c a/b/c/d a/b/c/d/e a/b/c/d/e/asdf a/b/c/d/e/5 a/b/c/d/e/1 a/b/c/d/e/ajkf a/b/c/d/e/3 a/b/c/d/e/2 a/b/c/d/e/4 $ rm -r a/b/c/d/e/a{sd,jk}f $ find a a a/b a/b/c a/b/c/d a/b/c/d/e a/b/c/d/e/5 a/b/c/d/e/1 a/b/c/d/e/3 a/b/c/d/e/2 a/b/c/d/e/4

    (I don’t know why “find” lists the folders in that order. :S)

    Bash shell editing with vi (put in your ~/.bashrc):

    set -o vi

    Now in your bash shell you can <Esc>, then hjkl, wbe, yp, xdc, etc. Type ‘#’ anywhere in normal mode to save what’s on your prompt for future use. Also, press v in normal mode to edit your command in vim, then <Esc>ZZ to execute that command.

    Instead of “history | grep mycmd”, you can also <Ctrl-r> and then type some substring of a previous command. Now <Esc> to bring to prompt, or <Enter> to execute.

    For extended command line history (put in your .bashrc):

    export HISTCONTROL=erasedups export HISTSIZE=10000

    Also, you can push directories onto a stack:

    ~/temp/a/b/c/d/e/1$ # oh crap, i have to do some stuff in another dir, but don’t want to lose my current location ~/temp/a/b/c/d/e/1$ pushd . ~/temp/a/b/c/d/e/1 ~/temp/a/b/c/d/e/1 ~/temp/a/b/c/d/e/1$ cd ../2 ~/temp/a/b/c/d/e/2$ # silly me, i am supposed to do something in another dir first before this dir ~/temp/a/b/c/d/e/2$ pushd . ~/temp/a/b/c/d/e/2 ~/temp/a/b/c/d/e/2 ~/temp/a/b/c/d/e/1 ~/temp/a/b/c/d/e/2$ cd ../3 ~/temp/a/b/c/d/e/3$ # now i finally do some stuff ~/temp/a/b/c/d/e/3$ popd ~/temp/a/b/c/d/e/2 ~/temp/a/b/c/d/e/1 ~/temp/a/b/c/d/e/2$ #now i do what i’m supposed to do here ~/temp/a/b/c/d/e/2$ popd ~/temp/a/b/c/d/e/1 ~/temp/a/b/c/d/e/1$ # now we’re back

  2. Do you know how to let cygwin store my commands, such that I could get them back after accidentally close my terminal? Every time I reopen cygwin and move up to find previous commands, I always find commands left by the previous co-op~lol


Leave a comment


No trackbacks yet.