Activities

August 2011
M T W T F S S
« Jul   Sep »
1234567
891011121314
15161718192021
22232425262728
293031  

Archiving/Moving files in a period using bash script

I have a requirement to archive the aged files inside a huge upload folder which is causing the limit of allowed files count on the shared hosting platform even though hosting plan supports unlimited storage. So the hosting service is alerting because of huge file count as well as client want to minimize it.

My suggestion is,

1. Modify the application to archive or delete the aged files in a period over it.
2. Move the files our and compress it to another folder.

But client wants a immediate action and I go with script option. Basic idea is we need to create a file with custom creation date and use find command to list out the files between the period, use “exec” command to move it out to another folder. Then compress it.

1. I want to list out the files in between Oct 2010 and March 2011. So I want to create both files with specified creation date. Check the syntax yyymmddhhmmss

$touch -m -t 201010010000 /tmp/startdate
$touch -m -t 201103010000 /tmp/enddate

2. Use find command to list the files and no. counts

$find ~/public_html/uploads/photo_images  -newer /tmp/startdate ! -newer /tmp/enddate
   $find ~/public_html/uploads/photo_images  -newer /tmp/startdate ! -newer /tmp/enddate  |wc -l

3. Now I want to take a backup of moving files as a precaution. (Optional)

 $ find .  -newer /tmp/startdate ! -newer /tmp/enddate -exec cp {} \ ~/archived_photos

4. Compressing the backup files to minimise the file count

 $tar -cjf ~/images_back.tar  ~/archived_photos/

4. Finally deleting the unwanted files between the dates

 $find  ~/public_html/uploads/photo_images  -newer /tmp/startdate ! -newer /tmp/enddate  -exec rm -v {} \;

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>