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?
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:
@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.
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:
Last edited by migurus; 06-12-2012 at 02:54 PM..
Reason: typo
@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:
@migurus
when I run cal without arguments, it only shows the current month.
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)
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)
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)
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)
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)
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)
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)
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)