Wednesday, September 2, 2015

Cleanup log file

Simple script to clean up log file
vi cleanlog

#-------------------------------------------------------
#!/bin/bash

# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "$0 must be run as root" 1>&2
   exit 1
fi

if [ "$1" ];then
   LOGDIR=$1
   if [ ! -d "$1" ];then
      echo "$1 not exist"
      exit 0
   fi
else
   LOGDIR="/var/log"
fi

echo "clean up folder $LOGDIR"
rm -vf $LOGDIR/*log.*
rm -vf $LOGDIR/*.gz
for i in {0..20};do
    rm -vf $LOGDIR/*.$i*
done

echo "fill zero all"
for i in $LOGDIR/*
do
    if [ -f "$i" ];then
        dd if=/dev/null of=$i 2> /dev/null
        fi
done

for i in $LOGDIR/*
do
    if [ -d "$i" ];then
        echo "clean up sub folder $i"
        rm -vf $i/*log.*
        rm -vf $i/*.gz
        for j in {0..20};do
            rm -vf $LOGDIR/*.$j*
        done
       
        echo "fill zero subfolder $i"
                for j in $i/*
        do
            if [ -f "$j" ];then
                 dd if=/dev/null of=$j 2> /dev/null
            fi
        done
    fi
done

#---------------------------------------------------------

chmod a+x cleanlog
Last Update: 160229

No comments:

Post a Comment