Ubuntu

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

  • October 6, 2016

執行以下命令按預期工作:

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"

輸出:

目前日期和時間:Thu Oct 6 16:14:29 WEST 2016

從昨天開始備份日誌,請稍候…

完成!

刪除昨天的日誌…

完成!

將此命令放在 bash 腳本中時我缺少什麼?

代替

-name '*"$DATETIME".log'

-name "*$DATETIME.log"

單引號非常強,雙引號允許 $VARIABLE 在裡面。

引用自:https://serverfault.com/questions/807517