创建tar文件并按当前日期命名 [英] Creating tar file and naming by current date
本文介绍了创建tar文件并按当前日期命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用bash创建备份脚本,以将文件夹中的内容焦油并将其移动到某处,但是我真的不知道该怎么做.
I'm trying to create a backup script in bash, to tar the contents of a folder and move the resulting file somewhere, but I don't really know how to do it.
#!/bin/bash
name="$date +"%y-%m-%d""
tar -zcvf $name code
但是结果是该文件仅被命名为+%y-%m-%d
.如何更改脚本以按预期的日期命名文件?
But the result is that the file is just named +%y-%m-%d
. How can I change the script to name the file by the date as intended?
预期输出:2013-08-29.tar
推荐答案
像这样:
name=$(date '+%Y-%m-%d')
tar -zcvf "$name.tar.gz" code
甚至是一行:
tar -zcvf "$(date '+%Y-%m-%d').tar.gz" code
如果要使用.tar
而不是.tar.gz
,请
放下-z
标志.
Drop -z
flag if you want .tar
instead of .tar.gz
.
如果您只希望一年中的两位数(用17
代替2017
),请使用%y
代替%Y
.
Use %y
instead of %Y
if you want just 2 digits of a year (17
instead of 2017
).
$()
用于命令替换.
这篇关于创建tar文件并按当前日期命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文