how to get what date was 28 days ago of the current system date IN UNIX

Hi,

Anybody knows how to get what date was 28 days ago of the current system date through UNIX script.

Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010,

date +%Y-%m-%d --date='28 day ago'

date +%Y-%m-%d --date='28 day ago'

With the above command it will give the current date...

I need what date it was 28 days ago from the todays date.

Have you executed that command? and what output you got?
No it is giving 28 days ago from the current date.

When I executed the above command I got

18-02-2010

That is February 18th of 2010.
This is 28 days ago from 18th March of 2010(today)

Hi thillai_selvan,

I suspect the command you have given might be OS dependent (the format you suggested available under GNU and it's part of sh-utils package) as it does not work in my case too.

 
avalon:/disk1/jvsh/TEST/TES>date
Thu Mar 18 16:39:47 IST 2010
avalon:/disk1/jvsh/TEST/TES>date +%Y-%m-%d --date='28 day ago'
2010-03-18

Reddy,

have a look here : date manipulation

and something also like this you can use :

perl -le 'print scalar localtime (time() - 86400 * 28);'

Will work only if it is GNU date.

Try a generic one which will work irespective of OS and date flavours..(If perl is available...:wink: )

 
var=`perl -w -e '$d=28*86400;@t=localtime (time -$d); printf "%d%.2d%.2d", $t[5]+1900,$t[4]+1,$t[3];'`
echo $var
find -mtime +28 -exec rm {} \; 

rm ??? :eek: :eek::eek::eek::eek:

This will just delete all files before 28 days..ie, it will delete all files before 1st of March...not jsut the files of March1st, as per the example mentioned.

Hope its clear for you.

The correct one should be

find . -mtime 28 -exec rm {} \;

Also, just go with

find . -mtime 28 -exec ls -l \;

before executing rm , just to make sure that the files listed are what you need to delete.

Thank you Dennis, When I execute the perl code it is giving the correct date stamp.

$
$ uname -a
SunOS ccluatdwunix2 5.10 Generic_141414-08 sun4v sparc SUNW,T5240
$
$ var=`perl -w -e '$d=28*86400;@t=localtime (time -$d); printf "%d%.2d%.2d", $t[5]+1900,$t[4]+1,$t[3];'`
$
$ echo $var
20100218
$

Could you please help me how can I get the date stamp like 18-Feb-2010.

DON'T YOU KNOW THAT IS THE ACTUAL REQUIREMENT.
READ THE FOLLOWING LINE OF THE POSTER's POST.
Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010,

FOR THE ABOVE QUESTION ONLY I REPLIED.

I KNOW HOW TO GET THE PREVIOUS 28TH DAY.
DONT YOU KNOW THAT THE COMMAND WHICH I GAVE WILL GIVE THE 28TH DAY AGO? :o:o

Try:

perl -w  -e '@m = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); $d=28*86400;@t=localtime (time -$d); printf "%2d-%s-%d", $t[3],$m[$t[4]],$t[5]+1900;'

---------- Post updated at 17:43 ---------- Previous update was at 17:32 ----------

Hello,

If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010,

I just said your command below will delete all files with mtime not just the exact 28th day before current date but also the files before that.ie, say all files on Jan, Feb of 2010, 2009 , 2008 etc...

find -mtime +28 -exec rm {} \; 

If the files has to be deleted which has got mtime eaxctly 28 days, you have to use

find . -name "*" -mtime 28

This does NOT match the requirement. You will get different results according to what time of day you run the command because the 28 days is measured from the current system time.
Also the command needs "-type f" and an understanding of the directory structure.

We need to know what the O/P is going to do with the date. Is it part of a filename or the actual directory timestamp of the file???

Btw: The perl solution at the top of post #12 appears to work to the latest version of the requirement.

once you got the date i hope you can do something like this, but surely there will be an easy approach.

da="20100218"
day=`echo $da |cut -c 7,8`
mon=`echo $da |cut -c 5,6`
yea=`echo $da |cut -c 1-4`

Mon=`cal $mon $yea | head -1 | awk '{ print substr($1,1,3) }'`

ddmonyyyy=$day"-"$Mon"-"$yea

echo $ddmonyyyy

Date calculation, yesterday, today, date format, epoc, ...

There has been lot of date calculation solution in this forum.

If you have Gnu date, then you can use previous date command.

If you have ksh93, then you can use ksh printf builtin
calculation including your format needs.

Read
Read more
And more

Some date calculation examples using ksh93 + printf:

#!/bin/ksh93
#date
printf "%T\n" now
printf "%(Today is %F or you can write %d.%m.%Y ...)T  \n" now

# save date to variable in standard format
timestamp=$(printf "%(%F+%H:%M:%S)T" now)
echo "timestamp:$timestamp"

# convert timestring as integer using variable
timestamp=$(printf "%(%Y-%m-%d+%H:%M:%S)T" now)
timeint=$(printf "%(%#)T" "$timestamp")
echo "timestamp:$timestamp"
echo "timeint:$timeint"
((daysec=24*60*60))
((yesterday=timeint - daysec))
((tomorrow=timeint + daysec))
# argument is # + date integer
#same = give integer using #char
printf "yesterday %(%Y-%m-%d)T  \n" "#$yesterday"
printf "tomorrow %(%Y-%m-%d)T  \n" "#$tomorrow"

sec19700101=$(printf "%(%s)T" "$d")
echo "sec since 1970-01-01 00:00 UTC: $sec19700101"
echo "= same as timeint:$timeint"

printf "day of week, monday=1: %(%u)T  \n" '#'$timeint
printf "weeknr, monday is first day of week: %(%V)T  \n" '#'$timeint

oldday=$(printf "%(%#)T"   "2008-08-15+00:00:00")
today=$(printf "%(%#)T"   "2009-08-15+00:00:00")
(( diff=(today-oldday)/(24*60*60)  ))
echo "diff:$diff"

((daysec=24*60*60))
dstr=20100228
y=${dstr:0:4}
m=${dstr:4:2}
d=${dstr:6:2}

day=$(printf "%(%#)T"   "$y-$m-$d+00:00:00")
((prev=day-daysec))
printf "%(%Y%m%d)T  \n" "#$prev"
day=$(printf "%(%#)T"   "$y-$m-$d")
((next=day+daysec))
printf "%(%Y%m%d)T  \n" "#$next"

You can download ksh93 for SunOS if you not have it.

Thank you one and all for your valuable solutions and guidence.