简体   繁体   English

使用shell保存带有日期和时间的文件

[英]Saving a file with the date and time using shell

I have something in Python which saves a file in the following datetime format: 我在Python中有一些东西可以将文件保存为以下日期时间格式:

>>> s = time.strftime('%Y%m%d%H%M%S')
>>> print s
20121208145544

Things have changed slightly and this now needs to be done in shell. 事情已经稍微改变了,这现在需要在shell中完成。 Is there a way to get the datetime in exactly the same format using shell? 有没有一种方法可以使用Shell以完全相同的格式获取日期时间?

Something like 就像是

touch filename_`/bin/date +%Y%m%d%H%M%S`.txt

creates an empty file 创建一个空文件

filename_20130104112845.txt

使用date命令 ,它采用完全相同的格式参数,并带有+

date +%Y%m%d%H%M%S

This works too: 这也适用:

>>> z = time.strftime('%Y%m%d%H%M%S')
>>> file_ = open(z+'.txt', 'w')
>>> file_.close()

This creates 20130104160557.txt. 这将创建20130104160557.txt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM