How to get previous month files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get previous month files
# 1  
Old 12-28-2005
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

Any ideas
# 2  
Old 12-28-2005
Use datecalc by Perderabo to get the date exactly 30 days ago. This is a fast date calculation script.

Just one question though, how are you going to be deleting files of dates like 30th/31st January seeing that February would never have those many days?
# 3  
Old 12-28-2005
Thank you but i don't want to use any script again...

My main aim is to get a string called "200511" into variable when the its dec 2005
and "200512" when its jan 2006 and '200601' when its feb 2006.

once i get it i can issue mdelete *pervmonth*.xls ( i have to logon to a FTP site and delete files).

In this case i don't think there is an issue of feb not having 30th and 31. becasue on feb 1st all jan files will be deleted.
# 4  
Old 12-28-2005
Try using this snippet:
Code:
#!/bin/ksh

year=$(date +%Y)
month=$(date +%m)

month=$(($month-1))
if [ $month -lt 1 ]; then
        year=$(($year-1))
        month=12
fi
echo $year$month

This should work. If you can, test the script by setting your system date to Jan 2006.
# 5  
Old 12-28-2005
Thank you it worked... but i have one more doubt

how can i set systemdate to Jan2006? once i set i can recvert it back right???

also now since its dec 2005 i am getting "200511" for previous month string.

when its feb to oct previous months will be single digit how can i append 0 to it

i.e i should ge 200601, 200602,200603 when its feb2006,march2006 and april 2006 respectively
# 6  
Old 12-28-2005
Am adding to blowtorch's solution.

Since it is ksh, use typeset.

Code:
#!/bin/ksh
typeset -Z2 month=00

year=$(date +%Y)
month=$(date +%m)

month=$(($month-1))
if [ $month -lt 1 ]; then
        year=$(($year-1))
        month=12
fi
echo $year$month


Last edited by vino; 12-28-2005 at 04:44 AM..
# 7  
Old 03-23-2006
We receive files daily and archive them in a directory... I want my script to check and delete all teh files that older than 3 months though my script... I have naming convention for the files... How can i do this???
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the previous month files using shell script?

could you please assist the below query. i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied. Please find the below directory structure:- ls -lrt total 1824... (2 Replies)
Discussion started by: venkat918
2 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

date command - getting previous month

Hi, On any given day, I want to capture the month that has gone by - said otherwise, how do I capture last month? expr date '+%m' - 1 Above expression is giving error. Please advise thanks ---------- Post updated at 09:28 AM ---------- Previous update was at 09:11 AM... (1 Reply)
Discussion started by: ab_2010
1 Replies

5. 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

6. 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

7. 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

8. 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

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
Login or Register to Ask a Question