Register
It is currently Wed Dec 17, 2014 7:16 pm

Need bash script to archive


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Oct 05, 2011 12:25 pm   

Joined: Wed Oct 05, 2011 11:47 am
Posts: 3
Hi All,

I'm not good in scripting and need to set this to my customer:

I need to archive files with name below to a differente location and leave only these where name containing date is less or equal to 9Months old.

Ex: these files need to be archived to different location:

drwxr-xr-x 2 root root 27686912 Dec 30 2010 att_201009
drwxr-xr-x 2 root root 29469696 Dec 30 2010 att_201010
drwxr-xr-x 2 root root 22671360 Jan 31 2011 att_201101
drwxr-xr-x 2 root root 25482240 Feb 18 2011 att_201011
drwxr-xr-x 2 root root 24310784 Feb 18 2011 att_201012
drwxr-xr-x 2 root root 1536 Feb 18 2011 att_200706
drwxr-xr-x 2 root root 45056 Feb 18 2011 att_200707


looking forward for your assistance.
br Bibi
OS Solaris 10


Top
 Profile  
 PostPosted: Thu Oct 06, 2011 1:51 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
If you are interested in file names, but not their last changed date, and the file names are of the form att_YYYYMM, this could do the job:
Code:
#! /bin/bash
date=$(date +%Y%m)
year=${date:0:4}
month=${date:4:2}
month=${month#0}
for file in att_[0-9][0-9][0-9][0-9][0-9][0-9] ; do
    f_year=${file:4:4}
    f_month=${file:8:2}
    f_month=${f_month#0}
    let diff=(year-f_year)*12
    let diff+=month-f_month
    if ((diff > 9)) ; then
        echo archive $file
    fi
done


Top
 Profile  
 PostPosted: Thu Oct 06, 2011 2:51 am   

Joined: Wed Oct 05, 2011 11:47 am
Posts: 3
Hi Choroba,

Really appreciate your help, thanks a lot.

I also forgot to say that this script will need to run from crontab and archive these files from locationA to locationB.
When i get time tonight, i'll try to play around with it but just in case you have some tricks :D

Thanks a million.


Top
 Profile  
 PostPosted: Thu Oct 06, 2011 9:52 am   

Joined: Wed Oct 05, 2011 11:47 am
Posts: 3
Ok Choroba, that was easy and it works great:D

Thanks You!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP