Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current year on the commandline [duplicate]

Tags:

date

bash

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)?

like image 448
Uli Köhler Avatar asked Aug 22 '15 23:08

Uli Köhler


People also ask

Which command will display the year from date command?

%D – Display date as mm/dd/yy. %Y – Year (e.g., 2020)

How do I get the current date in bash?

To get current date and time in Bash script, use date command. date command returns current date and time with the current timezone set in the system.

How do I display current year in Linux?

If you need the 2-digit year, you can simply use date +%y . Useful also to note that in Linux, the various options for date format can be found with man date , whereas in FreeBSD and other BSDs including OSX, you can see them with man strftime .


1 Answers

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.

like image 184
Uli Köhler Avatar answered Sep 27 '22 19:09

Uli Köhler