zip old /rotated logs in /var/log/messages file

#!/bin/bash
a=`find /var/log -name “messages.*” | grep -v gz | wc -l`
for (( i=0; i < $a; i++ ))
do
gzip -9 /var/log/messages.$i
done
ls -l /var/log/messages.*
find /var/log/ -type f -mtime +20 |grep -v *.gz | xargs rm -rf;

Unixplanetyary:# pwd
/var/log
Unixplanetyary#ls -ltr

-rw-r–r– 1 root adm 852480575 Jul 22 03:10 messages.9
-rw-r–r– 1 root adm 764759357 Jul 23 03:10 messages.8
-rw-r–r– 1 root adm 789044462 Jul 24 03:10 messages.7
-rw-r–r– 1 root adm 806885073 Jul 25 03:10 messages.6
-rw-r–r– 1 root adm 780612046 Jul 26 03:10 messages.5
-rw-r–r– 1 root adm 783171853 Jul 27 03:10 messages.4
-rw-r–r– 1 root adm 763277130 Jul 28 03:10 messages.3
-rw-r–r– 1 root adm 771602943 Jul 29 03:10 messages.2
-rw-r–r– 1 root adm 743181051 Jul 30 03:10 messages.1
-rw-r–r– 1 root adm 787018409 Jul 31 03:10 messages.0
-rw-r–r– 1 root adm 123195835 Jul 31 06:49 messgaes

After applying script output will be like this :

-rw-r–r– 1 root adm 448441364 Jan 9 03:10 messages.1.gz
-rw-r–r– 1 root adm 448886532 Jan 10 03:10 messages.0.gz
-rw-r–r– 1 root adm 9105430966 Jan 10 07:31 messages

Leave a Reply

Your email address will not be published. Required fields are marked *