2

I'm writing a simple batch downloader for files, which have the format Y-m-d.pdf. I want to pass the dates (from, to) as a parameter, e.g.:

./download.sh 2012-01-01 2012-01-31

That should download all files for January 2012.

Here's what I got so far:

#!/bin/bash for i in {0..9} do curl -u user:pw http://server/path/somescript.pl?date=`date -v-"$i"d +%Y-%m-%d` -o `date -v-"$i"d +%Y-%m-%d`.pdf done 

This downloads the ten most recent files.

EDIT: How can I iterate over a range of dates instead? Something like:

for d in {2012-01-01..2012-03-31} 

I was hoping for a built-in feature, otherwise I would need to take care of the number of days in each month (not to forget leap years) myself.

2
  • What exactly is your question? Commented Mar 31, 2012 at 7:48
  • I only figured out how to modify a date relative to the current date (e.g. date -v 10d), but I want to be able to get all dates between a start date and an end date. To put it in other words, how can I iterate over a range of dates? Commented Apr 1, 2012 at 8:00

2 Answers 2

3

The following shows the basic idea, you have to put the curl stuff in for yourself:

START=`date -j -f %Y-%m-%d:%H.%M $1:0.0 +%s` END=`date -j -f %Y-%m-%d:%H.%M $2:0.0 +%s` for (( i=$START; i<=$END; i+=86400 )); do # seconds/day echo `date -j -f %s "$i" +%Y-%m-%d` done; 

If you call it:

> ./range 2012-1-30 2012-2-4 2012-01-30 2012-01-31 2012-02-01 2012-02-02 2012-02-03 2012-02-04 
Sign up to request clarification or add additional context in comments.

1 Comment

Just adding the seconds to go to the next day, that's clever! Thanks a lot. Works like a charm.
1

YOU can use [01-31] to download a range of files:

$ curl -u user:pw 'http://server/path/somescript.pl?date=2012-01-[01-31]' -o '2012-01-#1.pdf' 

3 Comments

Thanks, I didn't know about this feature. It makes things a lot easier, but it doesn't solve the general case, in which the dates spread over multiple months or even years (and different number of days for different months must be handled).
You can have multiply [...]/{...} and -o '#1_#2'
'date=2012-[01-12]-[01-31]' -o '2012-#1-#2.pdf' I know, but how does this handle the fact, that there is a 2012-01-31 but no 2012-02-31?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.