0

I have situation where we have log files being saved in a /YEAR/MONTH/ folder structure, e.g. /2018/03/fakelog.tmp.gz

I'd like to have a cronjob run monthly to delete files older than a certain number of years, and instead of using say find I imagine it would be possible to make use of this structure to make things more efficient (with find -mtime it will take many many minutes to run).

I'm sure it'd be easy to get the current date and reduce the year by, say, 2 and then delete everything /2016/03/ but then we will miss anything in /2014/ or /2016/01 which could be a concern if the script is added to a different system, or for any reason doesn't run a specific month.

Is it possible to, for example, find any folders 'less than' 2016, or any folders 'less than' /2016/03 ? Or can this be solved in a different way that doesn't involve iterating through every combination.

Thanks for any help

Cesar
  • 103
  • 2

1 Answers1

2

I think it can be done with a simple script like the following:

#!/bin/bash

start_year=2010
year=`date +%Y -d '-2 year'`
mon=`date +%m -d '-2 year'`
prev_year=`expr $year - 1`

if [ $start_year -gt $year ]; then
    echo "Nothing to do"
    exit 0
fi

for i in `seq $start_year $prev_year`; do
    echo "Removing year [$i]"
done

for i in `seq 1 $mon`; do
    m=$(printf '%02d' $i)
    echo "Removing month [$year/$m]"
done

You need to set your starting year like '2010' as shown above. Here, the script will just show the folders to be deleted. This is safer to make sure and confirm you delete what you really need to. When satisfied, you can just added rm -rf dir_name.

Khaled
  • 36,533
  • 8
  • 72
  • 99