Search and tar Files By Date

By | 2015/05/08

The find command in Linux has an option -newerXY where a date can be specified. Using this in combination with some tar action will let you archive files by date!

Here is a quick one-liner example:

$ find /mnt/media/data/ -type f -name "*.txt"  -newermt '2011-03-01' ! -newermt '2011-04-01' | xargs tar -czvf 2011-03-`hostname`-archived.tgz --remove-files

This example finds files in /mnt/media/data/ which have the extension .txt and in the date range of the month of March 2011, inclusive. Those files are then archived to a tgz.
The –remove-files option will delete the files from the directory when they are placed into the .tgz.

Rock on,

Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.