Linux: Deleting old logs


We all know how annoying it can get to have old log files that are not really being used on our production server. Hence, it would be great to implement a script to perform the task on a cronjob so that we can relax as lazy system engineers 🙂 🙂

The script to delete files older than 15 days would be as follows:

#!/bin/bash

# File is saved as root, in /scripts/delete_old_logs.sh

# File needs to be chmod 777 to work

DAYS=15

PATH_TO_LOGS=/var/log

find $PATH_TO_LOGS -type f -mtime +$DAYS -exec rm -rf {} ;

Next, we can schedule it to run on a daily basis, every 11pm:

crontab -e

add this line

*    23    *    *    *    /scripts/delete_old_logs.sh

Gulshan Yashveer Beejan

Gulshan holds a BSc(Hons) Computer Science with Distributed systems from the City University of London. He is currently working at DSOGroup(Mauritius) as a Linux Administrator on SaaS Solutions. More about Gulshan

Leave a Reply

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