2
Feb

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

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

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 2 Comments So Far

  • offw0rld
    February 4th, 2010 at 1:48 pm

    Alternatly, wget http://website.com/images/step{1..20}.png

  • Derek Hildreth
    February 4th, 2010 at 7:51 pm

    I appreciate the input! Thanks! Didn’t realize it could be that simple…

Share your thoughts, leave a comment!