Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Archive old files more than a month in unix/solaris

Status
Not open for further replies.

lats

Programmer
Feb 3, 2017
1
IN
Hi,

I have files concatenated with day of the year in a folder.
Say as
Jan 1st - aaa.001.txt
Feb 3rd - aaa.034.txt etc.
which I get with `date +%j`
I now need to archive files older than a month. my program works for forward dates other than January.
Being in Feb, I can delete files older than 30 day by taking few constant values in variables and negating with current date value.
However, being in Jan, my approach fails to archive last year's data. i.e. 335 --> 365.

Note: the script should run when ever we trigger and it should ensure to retain 1 month's data and delete rest of the old files.

Please help. Thanks in advance.

I can paste my code copy if required.
 
Something like this maybe?

Code:
#!/bin/ksh

typeset -Z3 DAY=$(date '+%j')

print "Today's day-of-year: ${DAY}"

typeset -Z3 PREV=$(( (365 + DAY - 30) % 365 ))

print "One month ago: ${PREV}\n"

DAY=15

print "For DOY = ${DAY}"

PREV=$(( (365 + DAY - 30) % 365 ))

print "One month prior: ${PREV}"

Output is...
Code:
Today's day-of-year: 034
One month ago: 004

For DOY = 015
One month prior: 350

Of course you'd need to add adjustments for leap year, but this approach will get you 30 days prior to any day-of-year.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top