Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current date in YYYY-MM-DD format in (OS X) bash?

Tags:

Writing a shell script and I want to do something like this:

cp myfile.ext myfile.2011-06-10.ext 

where 2011-06-10 is the current date.

Thoughts?

like image 951
Alan H. Avatar asked Jun 10 '11 23:06

Alan H.


People also ask

How do I get 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 change the date format in bash?

Bash Date format YYYY-MM-DD To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.

What is the date command in bash?

Date command is an external bash program that allows to set or display system date and time. It also provides several formatting options. Date command is installed in all Linux distros by default. Type date command in terminal which will display current date and time.

Which command is used for displaying date in the format dd mm yyyy in Unix?

We can use the `date` command to display or change the current date and time value of the system.


1 Answers

cp myfile.ext myfile.`date +%Y-%m-%d`.ext 

or use shorthand format

date +%F 
like image 151
Nemo Avatar answered Sep 24 '22 08:09

Nemo