Loop over a range of numbers to download with wget

11

8

How can I write a bash script that is going to do the following:

URL = "example.com/imageID="
while (1..100)
   wget URL + $i #it will wget example.com/imageID=1, then 2, then 3, etc
done

So I have a number of loops to perform, a URL which is ended with a number from the loop. I need to wget all of these.

Szymon Toda

Posted 2012-08-04T15:57:46.307

Reputation: 1 239

Answers

19

if you are using Bash 4.0, you can do this:

wget example.com/imageId={1..100}.jpg

C.C.

Posted 2012-08-04T15:57:46.307

Reputation: 306

10

It's fairly easy, even for someone with not much programming experience. When in doubt always read the Bash manual:

for i in {1..100}
do
    wget "example.com/imageID=$i"
done

Canha

Posted 2012-08-04T15:57:46.307

Reputation: 486

As a matter of principle, you should enclose the $i in quotes: wget "example.com/imageID=$i" – Scott – 2018-09-24T05:22:33.810

this probably works great. however, i start from "00008000" and the wget shoots errors because for some reasn it skips the first four zeroes. – esaruoho – 2019-12-10T05:47:38.123

-1

In case variable is not at end of URL and variable is between underscores:
example.com/imageID_1_2018.gif
example.com/imageID_2_2018.gif
example.com/imageID_3_2018.gif
.
.
.
example.com/imageID_100_2018.gif

wget example.com/imageID_{1..100}_2018.gif
equivalently:
for ((i=1;i<=100;i++)); do wget example.com/imageID_${i}_2018.gif; done

Vassil

Posted 2012-08-04T15:57:46.307

Reputation: 1

3Welcome to Super User. Please [edit] your answer to provide more detail. Not everyone reading your post may understand how it answers the OP's question without a more detailed explanation. – I say Reinstate Monica – 2018-09-23T20:49:18.450

1I’m not sure this is an answer.  It looks like an explanation of the accepted answer (and not a very clear one). – Scott – 2018-09-24T00:21:03.610