Is there a way in unix to subtract a month or at least 30 days from a specific date? The date is to be inputted by the user.
2012-11-19 - 1 month = 2013-10-19
This one doesn't work:
date -d '2013-10-13 - 1 month' '+%F'
The easiest way is to convert the date to a unix time_t value (i.e. seconds since the beginning of the epoch, or '1-1-1970 00:00:00'), and then substract 30 days * 86400 seconds per day from that number.
e.g. the following example uses
Note that this depends on the GNU version of
my original answer here suggested plain awk, but further testing on my debian system has revealed that neither
If perl is installed on the system, perl can be used instead of awk:
There are several alternative methods and examples of doing this at:
Nov 1 2012 is used for illustration. That can be of any user input date.
Your example uses relative items in date strings which are a GNU date extension.
For example on Fedora 19 your example works as expected:
Thus, you can make sure that your script uses GNU date.