16

Using wget, I need to download a file whose URL contains the year. How can I embed the current 4-digit year (at the time of running the command, e.g. 2015)? For example, what would the command currentyear be in

wget "http://example.com/data-$(currentyear).txt"

What would be the corresponding command if I'd need the two-digit year (e.g. 15 for 2015)?

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • Really deserves downvote , but since I get to this question in a wrong way just duplicate. Searching for https://www.bing.com/search?q=bash+get+date gives answer among top 2 links. – Alexei Levenkov Sep 11 '15 at 03:29
  • @AlexeiLevenkov Why do you think this should be downvoted? – Uli Köhler Sep 11 '15 at 03:30
  • Search for title https://www.bing.com/search?q=bash%20Get%20the%20current%20year gives you immediate answer http://www.cyberciti.biz/faq/bsd-appleosx-bash-ksh-get-current-year-shell-script/ as well as couple of SO questions - I'm not sure what kind of research one would do to miss searching for title of own question. Edit you've made to the post about "YYYY-MM-DD" being soooo different from "YYYY" does not really make this post better... – Alexei Levenkov Sep 11 '15 at 03:35
  • @AlexeiLevenkov Well, I believe the information should also be on SO. Other links and sites might disappear so I don't think it should generally not be on SO just because there is a solution elsewhere. But if you want to downvote, please do so ;-) Regarding the duplicate: Yes, the difference is not very large. I didn't know of the other question before. However, it is a difference and not an exact duplicate. I was asked by the system to edit in what the difference was and I did clarify that. – Uli Köhler Sep 11 '15 at 03:38
  • @AlexeiLevenkov BTW thanks for clarifying why you think it's bad. I'd love if more people would do that. – Uli Köhler Sep 11 '15 at 03:43
  • 1
    OK, you might be right I think. It is certainly not unacceptable. For me it is probably "on the edge of a duplicate". My basic intention was to have something on SO that would be googleable. I'll close-vote for this being a duplicate then. – Uli Köhler Sep 11 '15 at 03:53

1 Answers1

45

You can simply use the date command as date +%Y:

wget "http://example.com/data-$(date +%Y).txt"

If you need the 2-digit year, you can simply use date +%y.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • 4
    Useful also to note that in Linux, the various options for date format can be found with [`man date`](http://linuxconfig.org/date-1-manual-page), whereas in FreeBSD and other BSDs including OSX, you can see them with [`man strftime`](https://www.freebsd.org/cgi/man.cgi?query=strftime). – ghoti Aug 23 '15 at 00:05