1

I want to use wget with a cron job to pull a file every day, however I want it to pull the previous day's file from the remote server and the file is controlled by date variables in the URL as such:

http://example.com/export/file.csv?DateRange=20150429,20150429

I can pull the current date using this:

/usr/bin/wget --output-document=/file.csv "http://example.com/export/file.csv?DateRange=`date +%Y%m%d`,`date +%Y%m%d`"

This file is no good, though; I want to pull yesterday's date and not today's.

Is there a way I can subtract 1 day?

2

If you have GNU date, you can do:

date -d yesterday +%Y%m%d

For example:

$ date -d yesterday +%Y%m%d
20150430
$ date +%Y%m%d      
20150501

The command would be:

/usr/bin/wget --output-document=/file.csv "http://domain.com/export/file.csv?DateRange=`date -d yesterday +%Y%m%d`,`date +%Y%m%d`"

Check the GNU documentation for more examples. You can also do: date --date='1 day ago' or date --date='-1 day'.

2
  • Thanks, this works perfectly in shell when i run it manually, but if i add it to a cron, it doesnt run. I figured out that with the cron the % needs escaping with ``
    – Mike Meade
    May 1 '15 at 13:16
  • @MikeMeade yep. The crontab manual says % is for marking a newline, or something like that.
    – muru
    May 1 '15 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.