title: reliable way to delete older files in unix shell script
body: <p>I'm trying to construct a reliable shell script to remove older files based on Xn of days using <code>find</code>. However, the script seems to work intermittently. Is there a better way? I list the files first to make sure I capture them, then use <code>-exec rm{}</code> to delete them. </p>
  
  <p>I execute the script like so:
  <code>/home/scripts/rmfiles.sh /u05/backup/export/test dmp 1</code></p>
  
  <pre><code>#!/usr/bin/ksh
  
  if [ $# != 3 ]; then
     echo "Usage: rmfiles.sh &lt;directory&gt; &lt;log|dmp|par&gt; &lt;numberofdays&gt;" 2&gt;&amp;1
     exit 1
  fi
  
  # Declare variables
  HOURDATE=`date '+%Y%m%d%H%M'`;
  CLEANDIR=$1;
  DELETELOG=/tmp/cleanup.log;
  
  echo "Listing files to remove..." &gt; $DELETELOG 2&gt;&amp;1
  /usr/bin/find $CLEANDIR -name "*.$2" -mtime +$3 -exec ls -ltr {} \; &gt; $DELETELOG 2&gt;&amp;1
  
  echo "Removing files --&gt; $HOURDATE" &gt; $DELETELOG 2&gt;&amp;1
  #/usr/bin/find $CLEANDIR -name "*.$2" -mtime +$3 -exec rm {} \; &gt; $DELETELOG 2&gt;&amp;1
  </code></pre>
  
  <p>My sample directory clearly has files older than one day as of today, but <code>find</code> is not picking it up when it was before during some previous testing.</p>
  
  <p><code>Thu Sep 26 08:54:57 PDT 2013</code></p>
  
  <pre><code>total 161313630
  -rw-------   1 oracle     dba        10737418240 Sep 24 14:17 testexp01.dmp
  -rw-------   1 oracle     dba        10737418240 Sep 24 14:20 testexp02.dmp
  -rw-------   1 oracle     dba        10737418240 Sep 24 14:30 testexp03.dmp
  -rw-------   1 oracle     dba            508 Sep 24 15:41 EXPORT-20130924.log
  -rw-------   1 oracle     dba            509 Sep 25 06:00 EXPORT-20130925.log
  -rw-------   1 oracle     dba            508 Sep 26 08:30 EXPORT-20130926.log
  </code></pre>