title: Bash - delete files by date/filename
body: <p>I have a bash script which creates a mysqldump backup every hour in a certain directory.</p>
  
  <p>The filenames of the backup files include the date and hour as per the following schema:</p>
  
  <pre><code>backupfile_&lt;day&gt;-&lt;month&gt;-&lt;year&gt;_&lt;hour&gt;.sql.gz
  </code></pre>
  
  <p>and to clarify here are some example filenames:</p>
  
  <pre><code>backupfile_30-05-2012_0800.sql.gz
  backupfile_01-06-2012_0100.sql.gz
  backupfile_05-06-2012_1500.sql.gz
  </code></pre>
  
  <p>Would someone help me with creating a script that will loop through all files in the directory and then delete files LEAVING the following:</p>
  
  <ol>
  <li>Keep alternate hour backups older than a day</li>
  <li>Keep twice daily backups older than a week</li>
  <li>Keep once daily backups older than a month.</li>
  </ol>
  
  <p>I have the following beginnings of the script:</p>
  
  <pre><code>#!/bin/bash
  cd /backup_dir
  
  for file in *
  do
      # do the magic to find out if this files time is up (i.e. needs to be deleted)
      # delete the file
  done
  </code></pre>