Visit Our UNIX and Linux User Community


Adding Previous Month To Filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Previous Month To Filename
# 1  
Old 07-16-2012
Adding Previous Month To Filename

Dear experts,

I'm using solaris 5.10 and bash. I want to zip file "Amount.txt" to "Amount.zip" and rename it to "Amount_<prev_month>_<this year>.zip". For example, file for this month should be renamed to "Amount_06_2012.zip", for next month it should be "Amount_07_2012.zip". I have no problem for zipping the file. Here's the script but it's not working:

Code:
month=`date +%m`
year=`date +%Y`
lmonth=`expr $month - 1`
if test "$lmonth" = "0"
then
lmonth=12
year=`expr $year - 1`
fi

mv Amount.zip Amount_`$lmonth`_`$year`.zip

When I executed it, it shows:
Code:
./script.sh: 6: not found
./script.sh: 2012: not found

Then the zip file renamed to "Amount__.zip"

Best regards,
Kris Adrianto
# 2  
Old 07-16-2012
For previous month use this...
Code:
date --date="last month" +%m_%Y
06_2012

# 3  
Old 07-16-2012
Use below command instead of your mv.
Code:
mv Amount.zip Amount_$lmonth_$year.zip

# 4  
Old 07-16-2012
Quote:
Originally Posted by pravin27
Use below command instead of your mv.
Code:
mv Amount.zip Amount_$lmonth_$year.zip

almost there.. but when I use this, it only shows: "Amount_2012.zip" not "Amount_6_2012.zip". Then I erased the $year, then it shows "Amount_6.zip".
# 5  
Old 07-16-2012
try this.....

Code:
L_Date=`date --date="last month" +%m_%Y`
mv Amount.zip Amount_$L_Date.zip

This User Gave Thanks to pamu For This Post:
# 6  
Old 07-16-2012
Quote:
Originally Posted by pamu
Code:
L_Date=`date --date="last month" +%m_%Y`
mv Amount.zip Amount_$L_Date.zip

Hi pamu.. i think your code isn't applicable in solaris with bash..
When I execute the first line of your code, it shows:
Code:
date: illegal option -- date=last month
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]


Last edited by kris.adrianto; 07-16-2012 at 06:46 AM.. Reason: typo ;)
# 7  
Old 07-16-2012
Code:
month=`date +%m`
year=`date +%Y`
lmonth=`expr $month - 1`
if test "$lmonth" = "0"
then
lmonth=12
year=`expr $year - 1`
fi

L_date=$lmonth"_"$year

mv Amount.zip Amount_$L_Date.zip

Now it will work ...

but from your script we are getting previous month as 6 not 06..
This User Gave Thanks to pamu For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #142
Difficulty: Easy
Version 4 Unix was rewritten in C in 1977.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

2. UNIX for Dummies Questions & Answers

Get the previous month from date

Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: dt=201211 Please help. :confused: (3 Replies)
Discussion started by: abhi_123
3 Replies

3. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

4. Shell Programming and Scripting

Function to get previous month

Can someone help me creating a function which will give me previous months. like for example if the date is 200902 and if i call my function and pass a parameter 2 i want to get 200812 as the answer. or if i pass 200902 with a parameter 7 then my function should give me the date as 200807. (1 Reply)
Discussion started by: Lincy
1 Replies

5. UNIX for Dummies Questions & Answers

How to get the previous month by using `date`

Hello, I'm new to shell scripting. We've develop a script which will grep a file on the search criteria, MON (Jan/Feb/Mar/etc). But we should set this sript in cron which will run on every first day of the month. The problem I'm having is, when I run the script, it is displaying the contents of... (7 Replies)
Discussion started by: suneelj
7 Replies

6. Shell Programming and Scripting

Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008). Here is what I have so far. #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if then ... (11 Replies)
Discussion started by: sirrtuan
11 Replies

7. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

8. Shell Programming and Scripting

How to get previous month files

Hi, My task to to delete files which are of previous months. I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files i.e in Dec i have delete all nov files which look like 200511DD.XLS in Jan i have to delete all Dec files 200512DD.xls ... (7 Replies)
Discussion started by: savitha
7 Replies

9. Shell Programming and Scripting

file name using previous month

This has probably been asked 100 times, but I couldn't find any articles on point. I have a script that runs on the last day of every month at 11:30pm. If cats a number of input tables that were created the previous month (or earlier), combines them into one master file and erases the indivual... (1 Reply)
Discussion started by: beilstwh
1 Replies

Featured Tech Videos