Move File Every Last Date of the Month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move File Every Last Date of the Month
# 1  
Old 06-12-2012
Move File Every Last Date of the Month

Dear experts,

I have a file named "GI_GR.csv" generated by SAP everyday. What I need is every last date of the month ( Jan 31, Feb 28/29, Mar 31, etc), the file is copied to subfolder named "archive" and rename it to "GI_GR_Jan.csv" for January and so on. What I want to ask is, how to check if it's the end of the month and rename the file adding month's name?

Regards,
Kris Adrianto
# 2  
Old 06-12-2012
Hi
Try using cron to schedule the process, once scheduled use
Code:
now=$(date +"%m_%d_%Y")
mv file /path/backup_$now.csv"

to rename the file with desired date.

HTH.

Last edited by jamie_123; 06-12-2012 at 11:57 AM.. Reason: correction
# 3  
Old 06-12-2012
Please remember to post what Operating System and version you have and what Shell you use.

One pretty portable way of finding the last day of the month:
Code:
last_day=$(cal | tr ' ' '\n'|grep -v '^$' |tail -1)
date_day=$(date +%d)
if [ $date_day -eq $last_day ]
then
        ## copy process
        ## rename process
fi


@kris.adrianto
I notice that @jamie123 has interpreted the post as meaning that you want to delete the original file. Do you?
Similarly @jamie123 has sort of made a point by using the full date. If the process is to run for more than a year, you will need Month and Year in the filename.
There is no way to schedule a cron for the last day of the month unless you put the date in manually. It is easier to schedule a cron to run a script daily and to detect whether it is the last day of the month in the script.

Last edited by methyl; 06-12-2012 at 12:09 PM..
# 4  
Old 06-12-2012
The method above using cal depends on the default behaviour of cal (as poster mentioned). Run cal without any arguments and ensure it shows only one month, then the method will work.

On some systems cal without arguments would show previous, current and next month, in that case the method can be augmented with running cal with current month as an argument:
Code:
cal $( date '+%b' )


Last edited by migurus; 06-12-2012 at 02:54 PM.. Reason: typo
# 5  
Old 06-14-2012
@jamie_123
thanks for the answer, but I don't need to rename the file everyday, just at the end of the month.

@methyl
I'm using Solaris 5.10 and bourne shell.
I just want to copy the file not to move it. Yes, I also need year in my filename, but I have no problem in changing the filename. I agree with you, it is easier to run the script daily and checking whether if its the last day of the month. But I still don't understand about the first line of the code you made, could you explain more about this line:
Code:
last_day=$(cal | tr ' ' '\n'|grep -v '^$' |tail -1)

@migurus
when I run cal without arguments, it only shows the current month.
# 6  
Old 06-14-2012
Hi kris,
you can use cron to do it once a month as well. Check out the man page.
# 7  
Old 06-14-2012
Quote:
Originally Posted by kris.adrianto
@jamie_123
But I still don't understand about the first line of the code you made, could you explain more about this line:
Code:
last_day=$(cal | tr ' ' '\n'|grep -v '^$' |tail -1)

1) cal writes the calendar entry for the current month to standard output.

Code:
         June 2012
Sun Mon Tue Wed Thu Fri Sat
                     1   2
 3   4   5   6   7   8   9
10  11  12  13  14  15  16
17  18  19  20  21  22  23
24  25  26  27  28  29  30

2) The tr command converts each space to a new-line, in the order in which they appear. This operation introduces some blank lines in the output.

Code:








June
2012








Sun
Mon
Tue
Wed
Thu
Fri
Sat























1


2

3


4


5


6


7


8


9
10

11

12

13

14

15

16
17

18

19

20

21

22

23
24

25

26

27

28

29

30

3) The grep command removes these blank lines. So, the last number(day) becomes the last line.
Code:
June
2012
Sun
Mon
Tue
Wed
Thu
Fri
Sat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

4) tail -1 picks up this last line.
Code:
30



PHEW..... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. UNIX for Dummies Questions & Answers

Need script to move files based on month

Hi , I need a script which moves files based on month. Example : Apr 29 03:16 log4.txt Apr 29 03:16 log5.txt May 4 09:17 log1.txt May 4 09:17 log2.txt Move Apr files into Apr2015(Folder) Move May files into May2015(Folder). This is urgent requirement , if you can help me... (5 Replies)
Discussion started by: rockingvj
5 Replies

3. Shell Programming and Scripting

File Move based on date

how to move file based on date in linux (1 Reply)
Discussion started by: syedzoniac
1 Replies

4. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

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

6. Solaris

Move files into different folders based on its month

Hi All, I want to move the files in to different folders based on the files month in the file timestamp. For example All the september files in the directory should moves into the folder "sep_bkp_files" , August files in to aug_bkp_files folder... Please help me to achive the above... (10 Replies)
Discussion started by: velava
10 Replies

7. Shell Programming and Scripting

Move file older date

hlow all i have folder with name like this 20110512 20110601 20110602 so i want move the last 1 day to other directory for example this date 20110602 so i want move folder older 1 day from this date 20110512 -----> this folder will move to other dir 20110601-----> this folder will move... (2 Replies)
Discussion started by: zvtral
2 Replies

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

9. Shell Programming and Scripting

How to find the create time of a file if current date is in next month

Hi All, I want to find the time diffrence between currnt time and "abc.txt" file create time. I have solve that but if the abc.txt file created last month then is there any process to find the difftent? Exp: Create time of abc.txt is "Apr 14 06:48" and currect date is "May 17 23:47".... (1 Reply)
Discussion started by: priyankak
1 Replies

10. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies
Login or Register to Ask a Question