To copy files which are created in particular month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To copy files which are created in particular month
# 8  
Old 07-16-2017
Thanks

My OS is SunSolaris and Shell is Bash

Is it possible to extract the month and year from the date which i pass as parameter and search for that files for copying from the directory
# 9  
Old 07-17-2017
If you can trust the parameter being passed in, then yes. That means trust the format and trust the content (i.e could it ever be YYYY-DD-MM, YYYY-Mon-DD or refer to 30th February etc.)

You might try:-
Code:
#!/bin/ksh
input_param=$1

YYYY="${input_param%%-*}"
MM="${input_param#*-}" ; MM="${MM%-*}"
DD="${input_param##*-}"

echo "The year is $YYYY"
echo "The month is $MM"
echo "The day is $DD"

This way of splitting up the string is called variable substitution. You can then touch a file for the start of the month and one for the end of 'today' from which you can then use find to list the files in between.

How far have you got? It would be better for you if we can help you build something you can support and adjust rather than just giving a working solution.


Kind regards,
Robin
# 10  
Old 07-18-2017
Hi,

I am using below code but not able to proceed how i find the files between these dates

Code:
#!/bin/ksh
input_param=$1

YYYY="${input_param%%-*}"
MM="${input_param#*-}" ; MM="${MM%-*}"
DD="${input_param##*-}"

touch start_${DD}_${MM};


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-18-2017 at 02:10 AM.. Reason: Added CODE tags.
# 11  
Old 07-18-2017
Between which dates? In port#10, you supply just one date, and in the file name only, not the file's meta data. What in post#7 doesn't satisfy your needs?
# 12  
Old 07-18-2017
The timestamp format for the touch command is usually YYYYMoDDHHMi.SS where .SS is optional, so if you know the year, month & day you want as YYYY, MM & DD respectively, you can do this:-
Code:
touch -mt ${YYYY}${MM}010000       /tmp/start_of_month_file
touch -mt ${YYYY}${MM}${DD}2359.59 /tmp/last_day_file

find /path/to/search -newer /tmp/start_of_month_file ! -newer /tmp/last_day_file

I just ran this with a parameter of $(date +%Y-%m-$d) for the current date in the required format and generated these two files that the find then used to list files with:-
Code:
-rw-rw-r-- 1 rbatte1 root 0 Jul 18  2017 /tmp/last_day_file
-rw-rw-r-- 1 rbatte1 root 0 Jul  1 00:00 /tmp/start_of_month_file

The timestamp for /tmp/last_day_file is in the future, hence the slightly odd display.

Use reference file names that make sense to you.


I hope that this helps, but what's wrong with RudiC's suggestions?

Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

3. Shell Programming and Scripting

Listing the file name and no of records in each files for the files created on a specific day

Hi, I want to display the file names and the record count for the files in the 2nd column for the files created today. i have written the below command which is listing the file names. but while piping the above command to the wc -l command its not working for me. ls -l... (5 Replies)
Discussion started by: Showdown
5 Replies

4. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

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... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

5. Shell Programming and Scripting

Copy an array to a newly created directory

hello everyone, I am new to perl script and trying to develop a script as follows. I am trying to Create an array for storing all file names. I am trying to copy $libs into "scratch". however i am unable to do so. Please suggest.. #!/usr/bin/perl use File::Copy; #use... (5 Replies)
Discussion started by: Rashid Khan
5 Replies

6. Shell Programming and Scripting

Copy files based on last created date

Hi, I have a requirement to copy files from a windows network drive to a Linux server using shell script based on the last created date. Ex: FileName CreatedDate/Time F1 05-01-2012 3:00 PM F2 05-01-2012 3:15 PM F3 05-01-2012 2:00 PM When i run the shell script... (1 Reply)
Discussion started by: Lee_10
1 Replies

7. Shell Programming and Scripting

To copy a a file to last created directory..Urgent pls

I need to copy a file example hhh.txt to the recently created directory by name flexite@latesttimestamp in the path interface/home/ ... I couldnt get the name of recently created directory .... first result of ls -lst ...that is ls -lst |head -2 gives the latest directory but i could not... (2 Replies)
Discussion started by: helloo
2 Replies

8. UNIX for Dummies Questions & Answers

Copy only files created between two dates

Hi all, I like to get two date values from command line, and I have to copy all the files created in between that dates in a particular folder to destination folder. The date comparison is little bit consusing me. Please help. Regards Sethu. (2 Replies)
Discussion started by: r_sethu
2 Replies

9. Shell Programming and Scripting

display files created in a particular month

hi, i m new to unix. I have been trying to find all the files in my home directory and its subdirectories that are created in the month of september. Can anyone please help me with this??? (1 Reply)
Discussion started by: t_harsha18
1 Replies

10. Shell Programming and Scripting

command unix to list all files created since n month ago

Hello, I want to list all files that were created since 3 month ago. it exist a unix command to do it ? thank you (8 Replies)
Discussion started by: yacsil
8 Replies
Login or Register to Ask a Question