Re: Deleting x months old files



On 2007-08-10, pankaj_wolfhunter@xxxxxxxxxxx wrote:
Means, if I want to delete files older than 4 months, I need to know
how many days were there in these
past 4 months (30, 31, 28).
How can I loop over these 4 months in order to come up with total
number of days?

No need to loop:

set -- 30 31 30 31 31 30 31 30 31 28 31 31 30 31 30
shift $(( 12 - ${month#0} ))
d4=$(( $1 + $2 + $3 +$4 ))

You will need to adjust for leap years; this function can tell you
whether an y given year is a leap year:

is_leap_year() { ## USAGE: is_leap_year [year]
ily_year=${1:-$( date +%Y )}
case $ily_year in
*0[48] |\
*[2468][048] |\
*[13579][26] |\
*[02468][048]00 |\
*[13579][26]00 ) _IS_LEAP_YEAR=1
return 0 ;;
*) _IS_LEAP_YEAR=0
return 1 ;;
esac
}


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages

  • Re: loop
    ... On Tue, 10 May 2005 at 06:59 GMT, Ralph Zajac wrote: ... before the loop begins. ... A Problem-Solution Approach, 2005, Apress ...
    (comp.unix.shell)
  • Re: Replace field with awk/sed
    ... I tried it in for loop but takes too long for a file with 10000 ... Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ...
    (comp.lang.awk)
  • Re: Problem with shell arithmetic
    ... On 2006-12-14, Luqman wrote: ... Any idea what might be wrong with the following loop? ... A Problem-Solution Approach (2005, Apress) ...
    (comp.unix.shell)
  • Re: Looking for Conversation
    ... On 30/04/2007 10:11, Frogman wrote: ... You might meet yourself and end up in a loop (or is it leap) you can't get out of. ... aka Smite, Emits, Times, Mites, Items, Metis, Stime. ...
    (uk.people.silversurfers)
  • Perform a task if result is a whole number
    ... I am trying to include some logic that will determine leap years. ... want to perform a given loop if the year is evenly divisible by 4. ... if year/4 = a whole number, ... What is the easiest way to accomplish this? ...
    (microsoft.public.excel.programming)