Segregate files based on the time they are created

Hi All,

I have scenario where I need to zip huge number of DB audit log files newer than 90 days and delete anything older than that. If the files are too huge in number,zipping will take long time and causing CPU spikes. To avoid this I wanted to segregate files based on how old they are and then zip them.

Below is the code that i have written to achieve the same:

cnt=`ls -l | wc -l`
if [ cnt -gt 10000 ] ;
w=80
while [ $w -gt 9 ]
do
mkdir -p mv2SA/$ {w}
find . -name "*.aud" -type f -mtime +${w} -exec mv {} mv2SA/${w}\;
w=$((w-10))
done
fi

If i have file count greater than 10000, I will create multiple directories and move the files into them based on their age.

All 80 days older files should go to mv2SA/80
All 70 days older files should go to mv2SA/70
.
.
.
All 10 days older files should go to mv2SA/10

But its not working as intended, all the files going to mv2SA/80 directory only. Rest all directories are remaining empty.

Can you guys help in segregating the files into multiple directories.

Note: Looking for a solution which works on both HPUX & Linux Systems.

Regards,
Veeresham

Does your shell (which you fail to mention) allow for "arithmetic evaluation"? And, looking at your code, I doubt any of the directories are being created.
A few questions regarding the overall logics:

  • Will this be run automatically ( cron ?)?
  • How often?
  • Are you aware that depending on when you run the script, files will end up in unpredictable locations?

Also, along with RudiC's questions:

  • if is followed by then , which is followed by statements and then a fi
  • Variable declaration does not need $ ; but variable usage needs a $ in front of it so [ $cnt -gt 10000 ] instead of [ cnt -gt 10000 ]