Linux 将时间戳附加到 Ubuntu 中的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12773853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:32:22  来源:igfitidea点击:

Append the time stamp to a file name in Ubuntu

linuxbashtimestamp

提问by user1710563

I have a bash script that I use to backup the contents of a SSD every day however I use the following command in the script to name the file

我有一个 bash 脚本,用于每天备份 SSD 的内容,但是我在脚本中使用以下命令来命名文件

zip -r ssd-$(date "+%b_%d_%Y").zip ../ssd

It already appends the month, day, and year to the file name however how can I modify that to also append the timestamp of the server onto the file name as well?

它已经将月、日和年附加到文件名中,但是如何修改它以将服务器的时间戳也附加到文件名上?

回答by choroba

Does the following do what you need?

以下是否满足您的需求?

date "+%b_%d_%Y_%H.%M.%S" 

回答by James Waldby - jwpat7

Add _%H_%M_%Sinto the date-format string as well. For example, date +%b_%d_%Y_%H_%M_%Sproduces a string like Oct_07_2012_17_57_36. For a shorter string, consider format %s; eg, date +%sproduces a string like 1349654346, the number of seconds since 1970-01-01 00:00:00 UTC.

也添加_%H_%M_%S到日期格式字符串中。例如,date +%b_%d_%Y_%H_%M_%S产生一个像 Oct_07_2012_17_57_36. 对于较短的字符串,请考虑格式 %s; 例如,date +%s产生一个像 1349654346 这样的字符串,这是自 1970-01-01 00:00:00 UTC 以来的秒数。