首页 > 解决方案 > 如何根据文件名 tar 文件?

问题描述

有一些文件/var/log 如下:

cron
cron-20200322
maillog-20200329
tallylog
bootscan.log
bootscan.log-20200115.gz
bootscan.log-20200116.gz

我想备份文件:cron;cron-20200322;bootscan.log;bootscan.log-20200115.gz;bootscan.log-20200116.gz单个文件 /mnt/log_backup.tar
然后,我使用 bash shell 脚本如下:

#!/bin/bash
backup_dir='/mnt'
tar -cpf $backup_dir/log_backup.tar -C /var/log cron*
tar -cpfr $backup_dir/log_backup.tar -C /var/log boot*

可悲的是,我收到如下错误:

tar: cron*: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
tar: Removing leading `/' from member names
tar: boot*: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

我的 shell 脚本有什么问题?

标签: bashtar

解决方案


在将文件夹更改为 /var/log 之前评估 * 的通配符。

检查How to make tar globbing with the 'change directory' 选项


推荐阅读