How to get a previous date
December 18, 2007 Scripting No CommentsThis one gave me a bit of grieve yesterday. I know i had done this before in a one-liner but couldn’t quite remember how to do it. Even google was unhelpful at first, delivering only large scripts. The best i found was a perl statement:
perl -e ‘print scalar(localtime(time - 86400)), “\n”‘
But this wasn’t quite what i was looking for. I trudged thru some of my old scripts and came across a “date -d” statement. I tried it on solaris 10 but that turns out is an illegal option. So back to google and i discovered gnudate is part of the coreutils package. After installing that i am glad to say i have the use of date -d to get a previous date.
amsterdam:/ # /usr/local/bin/date -d “-2 day”
Sun Dec 16 11:42:30 EST 2007
Unfortunately the script requires a numeric value for the month so still no opportunity to get this in a one-liner.