一個關於如何在指定文件大於1GB後,自動刪除的問題。
批處理代碼如下:
#!/bin/bash
# 當/var/log/syslog大於1GB時
# 自動將其備份,並清空
# 注意這裏awk的使用
if ! [ -f /var/log/syslog ]
then
echo "file not exist!"
exit 1
fi
if [ `ls -l /var/log/syslog|awk '{print $5}'` -gt $((1024*1024)) ]
then
cat /var/log/syslog >> ~/log/history # 將日誌備份
echo >> ~/log/history # 增加一個空行
date >> ~/log/history # 記錄時間
echo "-------------------------------------" >> ~/log/history
echo > /var/log/syslog # 清空
fi
文章源自:烈火網,原文:http://www.veryhuo.com/a/view/33350.html