查找文件和tar.gz结果列表

执行以下命令按预期工作:

find /path/to/logs/ \( -type f -name '*20161005.log' \) -print0 | tar -czvf /path/to/backups/backup_logs_20161005.tar.gz --null -T - 

但是,试图在bash脚本中使用这个,我得到一个空的.tar.gz文件:

 #!/bin/bash now="$(date)" printf "Current date and time: $now" ## working paths LOGSPATH="/path/to/logs/" BACKPATH="/path/to/backups/" ## date to work with DATETIME=`date -d "yesterday 13:00 " '+%Y%m%d'` ## find files matching the working date and .tar.gz the result printf "\nStarting yesterday logs backup, please wait..." find "$LOGSPATH" \( -type f -name '*"$DATETIME".log' \) -print0 | tar -czvf "$BACKPATH"backup_logs_"$DATETIME".tar.gz --null -T - printf "\ndone!" ## delete backup files printf "\nRemoving logs from yesterday..." find "$LOGSPATH" -type f -name '*"$DATETIME".log' -delete printf "\ndone!\n" 

输出:

当前date和时间:星期四十月六日16:14:29
从昨天开始日志备份,请稍候…
完成了!
从昨天删除日志…
完成了!

在bash脚本中放置这个命令时我缺less什么?

更换

 -name '*"$DATETIME".log' 

 -name "*$DATETIME.log" 

单引号非常强,双引号允许$ VARIABLE里面。