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

2010 February 2

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 buy me a soda if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that I hope you'll be interested in:


Here are some similar posts that you may be interested in:


2 Responses leave one →
  1. 2010 February 4
    offw0rld permalink

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

    • 2010 February 4

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

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS