Move files by date range – particularly useful for filing large amounts of email

I wanted to clean up my Sent Items folder in my email – I don’t have time to go through and manually sort them, nor do I have time to delete some and keep others. My main aim was to break them up so that I didn’t end up with over 20k items in a single folder.

What I decided to do was move them into folders based on date – Kerio Connect stores emails as .eml files in the filesystem, so individual emails are easy to deal with as files, rather than being forced to talk imap to it when I’ve got local access.

I went into the Kerio Connect Client and made my destination folder structure – A top-level folder called Sent Archive and then folders underneath this by year – 2009, 2010 etc.

Then, to move them all to where they needed to be I stopped Kerio Connect and then ran the following shell commands:

cd /usr/local/kerio/mailserver/store/mail/\ Items/#msgs
find . -type f -newermt 20090101 -not -newermt 20100101 -exec mv {} ../../Sent\ Archive/2009/#msgs/ \;
find . -type f -newermt 20100101 -not -newermt 20110101 -exec mv {} ../../Sent\ Archive/2010/#msgs/ \;
find . -type f -newermt 20110101 -not -newermt 20120101 -exec mv {} ../../Sent\ Archive/2011/#msgs/ \;
find . -type f -newermt 20120101 -not -newermt 20130101 -exec mv {} ../../Sent\ Archive/2012/#msgs/ \;
find . -type f -newermt 20130101 -not -newermt 20140101 -exec mv {} ../../Sent\ Archive/2013/#msgs/ \;
find . -type f -newermt 20140101 -not -newermt 20150101 -exec mv {} ../../Sent\ Archive/2014/#msgs/ \;
cd /usr/local/kerio/mailserver/store/mail/
find Sent* -name index.fld -execdir mv {} index.bad \;

I then restarted Kerio Connect and had it reindex the folders and everything was looking good.

This article was posted by Kai Howells. If you liked this content and have any technical work in the Melbourne area, say hello via my contact form or give me a call on 0419 361 653 - I cover most of the greater Melbourne area and my rates are competitive.

Leave a Reply

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