我正在尝试编写一个Unix脚本,该脚本将转到特定目录(/ tmp / Sanbox / logs)并tar.gz该目录中的所有日志文件,并删除超过2天的原始文件。到目前为止,我有这个,但它不起作用。任何帮助将不胜感激。
#!/bin/bash
AGE_TO_COMPRESS="172800" # 172800 seconds = 2 days
LOG_FILES="export/home/H0166015/Sandbox/logs"
# Any file older than EDGE_DATE must be compressed
NOW=$( date +%s )
EDGE_DATE=$(( NOW - AGE_TO_COMPRESS ))
for file in $LOG_FILES ; do
# check if file exists
if [ -e "$file" ] ; then
# compare "modified date" of file to EDGE_DATE
if [ $( stat -c %Y "$file" ) -gt ${EDGE_DATE} ] ; then
# create tar file of a single file
tar -cvzf $file.tar.gz $file --remove-files
fi
fi
done