How can I get the current date in YYYY-MM-DD format in (OS X) bash?
Writing a shell script and I want to do something like this:
cp myfile.ext myfile.2011-06-10.ext
开发者_JS百科
where 2011-06-10 is the current date.
Thoughts?
cp myfile.ext myfile.`date +%Y-%m-%d`.ext
or use shorthand format
date +%F
Try
cp myfile.ext myfile.`date "+%Y-%m-%d"`.ext
and for a proper solution, decompose the name first into basename and extension, assign the date and then reassemble for final targetname.
The key is that
date +FORMAT
allows for very rich format strings. See date --help
or the rather fine manual.
The man page for the date
utility contains examples of how to output the date in various ways.
cp myfile.ext myfile.$(date +%F).ext
精彩评论