开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜