This question already has an answer here:
I am having trouble with using date as a variable in a bash script. I will admit that I have zero experience writing anything – in bash or otherwise – but I’ve been tasked with figuring this out. I have seen some other posts on here that come close to what I am looking for, but I haven’t been able to figure it out yet.
I need to move hundreds of log files from one directory to an archive directory named with the year and month (numerically) of last month, eg.,
2016-07. I want the script to look in the archive directory to see if the directory for last month is there and, if not, create it and then move the logs into it. I’m using RHEL 5.11.
From the command prompt this gives me what I need:
date +%Y-%m –d “last month”
I just can't get it work as a variable. I've tried the following (amongst other things):
last_month=date +%Y-%m -d "last month" last month=(date +%Y-%m -d "last month")