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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Gixxy22
  • 123
  • 1
  • 5

1 Answers1

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'.

muru
  • 72,889
  • 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 \ – Gixxy22 May 01 '15 at 13:16
  • @MikeMeade yep. The crontab manual says % is for marking a newline, or something like that. – muru May 01 '15 at 13:18