Delete files between two dates

filesrhel

I have a folder in which there are very old files. It contains files since 2009 and they are dump files with error log.

What I want to know is if it is possible to delete the files between let's say 2009 and 2011. Something like:

delete 'file_patern' between 2009-2011 and 2012-2014

I want to preserve for example 2010, 2015 and current year.

Machine is running on RedHat.

Best Answer

To list all files that were modified between 2009 and 2011, use find with -newermt:

 find . -type f -newermt '01 jan 2009 00:00:00' -not -newermt '01 jan 2012 00:00:00' -ls

To remove:

find . -type f -newermt '01 jan 2009 00:00:00' -not -newermt '01 jan 2012 00:00:00' -delete