Count in Sequence from X to Y in Terminal with seq Command

2010 February 2
by SendDerek

You can count up or down easily in the terminal using the command seq.  Combine this with a for loop and you have a really quick and easy way to count from x to y for the variable i.  For example, if I wanted to download all images that have been numbered step1.png, step2.png, step3.png, …, step20.png I could use the following command to download them:

for i in $(seq 1 20); do wget http://website.com/images/step$i.png; done

Mac OS Users:
Unfortunately, ‘seq’ isn’t included on Mac OS, so you’ll need to use the ‘jot’ program or cleverly use the ‘jot’ program in a script to mimic the ‘seq’ command and syntax like so (thanks to Fredrick Rodland for this one):

#!/bin/sh
# Fredrik Rodland
# dev_____AT____rodland.no
# http://rodland.no
# 20081004

MIN=$1
MAX=$2
PAD=$3
LENGTH=${#MAX}

if [ $PAD ]; then
     W="-w %0$LENGTH""d"
fi

let NMB_STEP=$MAX-$MIN+1
jot $W $NMB_STEP $MIN

If you’re a Mac user, I would recommend that you copy and save this to a file, save it as seq, make it executable with ‘chmod +x seq’, and finally copy it to your /usr/local/bin directory with ‘cp seq /usr/local/bin’.

Example of Search and Replace New Line/Special Characters in vi

2010 February 1
by SendDerek

Today I learned a few more things about vi while working on an HTML page like how to replace with \n (newline). Basically, you need to escape special characters with a backslash “\” but the < and > characters don’t need to be escaped. To give a newline, you simply hit which will give you “^M”. So, my full search and replace command looked like this:

:%s/<\/td>/<\/td>^M/g

Breaking it down… The “%” tells vi to apply the following command to the entire document. The search and replace command is s/search/replace/g where the “g” means global (replace all instances of the search term with the replacement term, not just the first instance).  In the </td> HTML tag, the forward slash must be escaped with the backslash character and the newline character ^M is created using <ctrl+v><ctrl+m>.

Text Wrapping in Vi

2010 January 29
by SendDerek

A really quick way to wrap text to a defined number of columns in ‘vi’ is to use

:%!fmt

The “%” selects the entire document, the “!” replaces the paragraph with output from the ‘fmt’ program. The ‘fmt’ program will, by default, wrap lines to 80 columns. If you’d like something different, you can simply type the number of columns like so

:%!fmt 50

Take a look at the ‘fmt’ man page for more options. Also, this :%! trick doesn’t just work with fmt, it works with programs like awk too. Have fun!

Apparently, “{!}fmt” will only wordwrap the paragraph which can be incredibly handy… I haven’t been able to reproduce the success from Mac OSX with VIM. Thoughts?

Extract/Uncompress/Unarchive Almost Any File in Linux (tar, tar.gz, tar.bz2, gz, bz, zip, 7z, rar, etc…)

2010 January 28

This is a no-frills Linux command line guide/cheat sheet that will help you extract or unarchive or uncompress just about any file that you’re bound to come across. If you’d like to have more options, read the man pages! Also, the opposite to this guide about creating/compressing/archiving files in Linux can be found here.
read more…

Writing a .dd.bz2 Image Directly to Media

2010 January 28
by SendDerek

Here’s a tip that I used just about everyday at work.  We usually compress our .dd images down using bzip2 and it can be a hassle having to uncompress and then dd the image to the SD card or other media.  Instead, why not do it all in one swift command with bz2cat?

bz2cat path/to/the/filename.dd.bz2 | dd of=/dev/sdb