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

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?

+6
source share
4 answers
 cp myfile.ext myfile.`date +%Y-%m-%d`.ext 
+16
source

Try

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

and for the correct solution, first decompose the name into the base name and extension, assign a date and then reassemble for the final target name.

The key is that

  date +FORMAT 

allows you to use very rich format strings. See date --help or a fairly accurate guide.

+6
source

The man page for the date utility contains examples of how to display the date in various ways.

+3
source
 cp myfile.ext myfile.$(date +%F).ext 
+2
source

Source: https://habr.com/ru/post/890263/


All Articles