in Programming Tools

Commands of the Week 20120521

Using less with colorized output

I use the colordiff command-line program to colorize diff output from diff/svn. I ran into a particularly long diff and tried to use less to read it. The less output was missing all the color. Using the -R option fixes that.

-R Repaint the screen, discarding any buffered input. Useful if the file is changing while it is being viewed.

Example command:

diff file1 file2 | colordiff | less -R

Source: //kb.ucla.edu/articles/how-do-i-keep-color-output-when-paginating-shell-output-through-less

Serving static files locally

Sometimes I just need to serve a directory in order to test a JavaScript app. Rather than fire up apache (which is what I used to do), you can run one command that will serve your current directory.

python -m SimpleHTTPServer 9090

Source: //www.lylebackenroth.com/blog/2009/05/03/serve-your-current-directory-using-a-simple-webserver-python/

SSH Background Processes

I wanted to kick off a few background jobs on a few different ssh hosts. It turned out to be trickier than I thought:

ssh user@target "nohup myprogram > foo.out 2> foo.err < /dev/null &"

Source: //stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine

Brace Expansion

If you’ve ever scripted a bash loop to run a command repeatedly with different arguments, brace expansion may be just the trick. For example, let’s say you want to create log directories log1, log2, …, log100. It’s a toy example, but I’m sure you’ve done something like this. You could do this with the following bash for loop:

for i in {1..100}
do
  mkdir log$i
done

An alternative way that is more concise is:

mkdir log{1..100}

You can also use multiple braces, which will give you a cross-product of commands

echo {a,b,c}{1..9}