Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

To copy files which are created in particular month

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
Master_Mind Master_Mind is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 18 July 2017, 12:27 AM EDT
Posts: 21
Thanks: 10
Thanked 0 Times in 0 Posts
To copy files which are created in particular month

Hi GUys,

I need to copy the files which are created on particuar month

for eg

Code:
ls dir
Jul 12 12:46 apple.txt
Jun 16 15:58 file.txt

i need to copy only files which are created on current month


Moderator's Comments:
To copy files which are created in particular month Please use CODE tags as required by forum rules!

Last edited by RudiC; 1 Week Ago at 04:05 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 1:22 AM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,408 Times in 3,138 Posts
Creation times are not stored in many *nix file systems. Did you consider the find command with its -mtime or -mmin options?
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 14 July 2017, 12:02 PM EDT
Posts: 166
Thanks: 55
Thanked 0 Times in 0 Posts
I just need to consider files which are created on current month taking year into consideration because my script will trigger on every month or when ever the script trigger take current month files
    #4  
Old Unix and Linux 1 Week Ago
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 21 July 2017, 10:53 AM EDT
Location: Chicago
Posts: 1,156
Thanks: 694
Thanked 15 Times in 14 Posts
Assuming you run the below every month, maybe:


Code:
find . -mtime -31 -exec cp {} targetdir \;


-exec would copy every result from the last month returned by find to the targetdir
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 24 July 2017, 10:22 AM EDT
Location: Lancashire, UK
Posts: 3,168
Thanks: 1,334
Thanked 604 Times in 546 Posts
Using a fixed -mtime value might get more files than you are expecting.

Try creating two reference files with touch and specify the timestamp as a date that will set delimiters for the age range of your files, then use find to get the files modified between the two reference files.

Would this approach help? What have you tried so far?

You haven't told us your OS version, so do you have the GNU date available so you can do date -d 'yesterday' and similar things?


We can probably help you achieve what you want, but you need to be clearer in the specification.



Regards,
Robin
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
Master_Mind Master_Mind is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 18 July 2017, 12:27 AM EDT
Posts: 21
Thanks: 10
Thanked 0 Times in 0 Posts
Hi Guys,

Basically i need to pick the files which are created in current month. i will pass date as parameter in my script


Code:
copy_scrtipt.sh YYYY-MM-DD

so based on this i need to pick the files which are created in that month
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 1:22 AM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,408 Times in 3,138 Posts
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Try

Code:
touch C1 -d${1:0:7}-01
touch C2 -d$(date -d"${1:0:7}-01 +1month -1day")
find . -newer C1 \! -newer C2

and don't complain date doesn't work this way on your system as you didn't mention any details of that...


EDIT:
Use $(date -d"${1:0:7}-01 +1month) for touching C2 as this will create it at midnight of next month's first, so that last month's entire last day is taken into account.

Last edited by RudiC; 1 Week Ago at 04:47 AM..
The Following 2 Users Say Thank You to RudiC For This Useful Post:
Master_Mind (1 Week Ago), rbatte1 (1 Week Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need last month files after 10th of every month nani1984 Shell Programming and Scripting 9 07-18-2014 04:56 PM
Copy files based on last created date Lee_10 Shell Programming and Scripting 1 05-29-2012 06:13 AM
Copy only files created between two dates r_sethu UNIX for Dummies Questions & Answers 2 07-27-2007 03:24 AM
display files created in a particular month t_harsha18 Shell Programming and Scripting 1 10-03-2005 01:22 AM
command unix to list all files created since n month ago yacsil Shell Programming and Scripting 8 12-15-2003 12:12 PM



All times are GMT -4. The time now is 02:38 AM.