I am trying to download the yesterday logs through an external API, For API I need to provide a date range from & to as input parameters.

From API I can download 1 hour log max for each call.

So if I want to download yesterday complete log. I need to call the API for 24 times with yesterday date(every hour time).

Note API will support only GMT time only. So need to provide GMT time.

Eg :- https://abcd.com/logs?start=FROM_DATE&end=TO_DATE

  1. First time loop it should go like below,
  2. https://abcd.com/logs?start=29-04-2018T00:00:00Z&end=29-04-2018T00:59:59Z
  3. Second time loop it should go like below
  4. https://abcd.com/logs?start=29-04-2018T01:00:00Z&end=29-04-2018T01:59:59Z
  5. Last time loop like below,
  6. https://abcd.com/logs?start=29-04-2018T23:00:00Z&end=29-04-2018T23:59:59Z

This script i will add in cron schedule, so everyday once it will get triggered and download yesterday 24 hour log complete.

Key points are

  1. Yesterday date always
  2. Format GMT
  3. 24 times loop

The below mentioned answers are throwing error. I started with this script, it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time.

FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"%H":00:00""Z")
for i in {0..3}
    echo "FROM_DATE : $FROM_DATE"
    TO_DATE=$(date -u +%Y-%m-%d"T"%H":59:59""Z" -d "1 day ago""$date + $i hour")
    echo "TO_DATE : $TO_DATE"
    FROM_DATE=$(date -u +%Y-%m-%d"T"%H":00:00""Z" -d "1 day ago""$date + $i hour""$date + 1 hour")
  • it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
    – Jhony
    Apr 27, 2018 at 12:12
  • Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
    – Philippos
    Apr 27, 2018 at 12:44
  • that is correct. I took hour and appended the minutes and seconds.
    – Jhony
    Apr 27, 2018 at 13:09
  • Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
    – Jhony
    Apr 27, 2018 at 13:10

2 Answers 2


Try this.

yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in {0..23}
    printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Z\n" $yesterday $hour $yesterday $hour

It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00 to 23:59:60, but ensure that your target application can also handle this:

yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')

for hour in {0..23}
    [[ hour == 23 ]] && lastsecond=$leapsecond
    printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZ\n" $yesterday $hour $yesterday $hour $lastsecond
  • the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
    – Jhony
    Apr 27, 2018 at 13:17
  • Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments. Apr 27, 2018 at 13:18
  • i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)\n 2018-04-26
    – Jhony
    Apr 27, 2018 at 13:18
  • @Jhony don't use echo where I've used printf. They're different commands for a reason. Apr 27, 2018 at 13:18
  • i have created script file with the above code as test.sh and running in linux environment
    – Jhony
    Apr 27, 2018 at 13:19

I am getting as expected output.

for i in {00..23}
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d '\r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d '\r'

Thanks a lot for your help

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .