简体   繁体   English

使用 crontab 重命名和移动文件

[英]Renaming and moving file using crontab

I want to make a backup of my history.txt file, where I store some information of my system.我想备份我的 history.txt 文件,在其中存储我系统的一些信息。 I would like to do this using the crontab and this is what I have now in my crontab:我想使用 crontab 来做到这一点,这就是我现在在我的 crontab 中所拥有的:

0 * * * * cp -a history.txt "history-$(date + "%Y%m%d-%h%m%s")" ; mv "history-$(date + "%Y%m%d-%h%m%s")" l-systems

Like you can see i want to preform the backup every hour and I want to give the file a name with the date.就像你看到的,我想每小时执行一次备份,我想给文件一个带有日期的名称。 First I make a cp of the file and rename it.首先,我制作文件的 cp 并重命名它。 After that I try to move the new file in a directory called l-systems.之后,我尝试将新文件移动到名为 l-systems 的目录中。 This doesn't work right now, can someone help?这现在不起作用,有人可以帮忙吗?

I would advise you to make a backup shell script like so:我建议您制作一个备份 shell 脚本,如下所示:

#!/bin/sh

DATE=$(date + "%Y%m%d-%h%m%s")
cp -a history.txt "history-$DATE"
mv "history-$DATE" l-systems

The call this script from crontab.从 crontab 调用这个脚本。 Since we use the same variable twice it will always be the same for both commands regardless of how long each takes由于我们两次使用相同的变量,因此无论每个命令需要多长时间,两个命令都将始终相同

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

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