gpt4 book ai didi

linux - 如何防止 tar 覆盖现有存档?

转载 作者:IT王子 更新时间:2023-10-29 01:06:34 25 4
gpt4 key购买 nike

我在 Ubuntu/Linux 上每天使用命令 tar -cpvzf ~/Backup/backup_file_name.tar.gz directory_to_backup/ 备份文件几次,(文件名包含日期 YYYY MM-DD 格式和从 a 到 z 的字母 - a 是该日期的第一个备份等)但我想创建一个新的存档,如果它已经存在则不覆盖存档。如何防止 tar 覆盖现有存档?如果存档存在,我希望 tar 不执行任何操作就退出(如果可能,显示一条错误消息)。

最佳答案

预先检查文件是否存在:

if [ -f ~"/Backup/[backup_file_name].tar.gz" ]; then
echo "ooops backup file was already here"
exit
fi
tar -cpvzf ~/Backup/[backup_file_name].tar.gz directory_to_backup/

请注意,~ 必须在双引号之外,如果您希望它被扩展。


更新

Thanks. Do you know how to make the archive file name and directory to backup as command line arguments? The file name includes the full path.

可以使用$1$2等来表示参数。例如:

if [ -f $1 ]; then
echo "ooops backup file was already here"
exit
fi
tar -cpvzf $1 $2

然后调用脚本:

./script.sh file backup_dir

关于linux - 如何防止 tar 覆盖现有存档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21680601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com