Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to get "last month"

SOLVED
Go to solution
Highlighted
Coolmar
Esteemed Contributor

Script to get "last month"

Hi,

I am creating a script that requires me to move files from "last month". So when ls -la the files and grep on `date +%b` (Dec)...what I actually want it to show is not this month but last month. Is there a way to get that value? It would be pretty simple if the dates were in numerical (Dec = 12) but with them in alpha it makes it more difficult...but I figure with you guys, not impossible ;0)

Thanks!
5 REPLIES
Patrick Wallek
Honored Contributor
Solution

Re: Script to get "last month"

Here's a shell script that will do it:

#!/usr/bin/sh

THISMONTH=$(/usr/bin/date +%m)

if (( $THISMONTH == 1 )) ; then
LASTMONTH=12
else
((LASTMONTH=${THISMONTH}-1))
echo "${THISMONTH} ${LASTMONTH}"
fi

case $LASTMONTH in
1|01) LASTMONTHTXT=JAN ;;
2|02) LASTMONTHTXT=FEB ;;
3|03) LASTMONTHTXT=MAR ;;
4|04) LASTMONTHTXT=APR ;;
5|05) LASTMONTHTXT=MAY ;;
6|06) LASTMONTHTXT=JUN ;;
7|07) LASTMONTHTXT=JUL ;;
8|08) LASTMONTHTXT=AUG ;;
9|09) LASTMONTHTXT=SEP ;;
10) LASTMONTHTXT=OCT ;;
11) LASTMONTHTXT=NOV ;;
12) LASTMONTHTXT=DEC ;;
esac

echo "LAST MONTH = ${LASTMONTHTXT}"
Robert-Jan Goossens_1
Honored Contributor

Re: Script to get "last month"

Hi Coolmar,

How about using the find command in combination with some reference files?

# touch -m -t 200611010001 /var/tmp/first
# touch -m -t 200611312359 /var/tmp/last

# find . -type f \( -newer /var/tmp/first -a ! -newer /var/tmp/last \) -print

Best regards,
Robert-Jan
Kent Ostby
Honored Contributor

Re: Script to get "last month"

Create a shell script:

#!/usr/bin/sh
LASTMONTH=`date +%b | awk -f useit.awk`
ls -la | grep $LASTMONTH

Create a file called useit.awk:

BEGIN{damonth[1]="Jan";damonth[2]="Feb";damonth[3]="Mar";damonth[4]="Apr";
damonth[5]="May";damonth[6]="Jun";damonth[7]="Jul";damonth[8]="Aug";damonth[9]="
Sep";damonth[10]="Oct";damonth[11]="Nov";damonth[12]="Dec";}
{for (idx1 in damonth)
{if (damonth[idx1]==$1)
{if (idx1==1){idx=13}
print damonth[idx1-1];}
}
}

NOTE: In the shell script, you'll need to put the full path of the file "useit.awk."

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Geoff Wild
Honored Contributor

Re: Script to get "last month"

ARCDIR=/some/place

# get the current month

M=`date +%b`

for FILE in `ls -al |awk '$6ne"$M"{print $9}'`
do
mv $FILE $ARCDIR
done

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Coolmar
Esteemed Contributor

Re: Script to get "last month"

Knew I could count on you guys! Thanks :0)