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
    #8  
Old Unix and Linux 07-16-2017
Master_Mind Master_Mind is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 21 September 2017, 4:32 AM EDT
Posts: 25
Thanks: 12
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #9  
Old Unix and Linux 07-17-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 26 September 2017, 9:43 AM EDT
Location: Lancashire, UK
Posts: 3,263
Thanks: 1,394
Thanked 631 Times in 570 Posts
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
Sponsored Links
    #10  
Old Unix and Linux 07-18-2017
Master_Mind Master_Mind is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 21 September 2017, 4:32 AM EDT
Posts: 25
Thanks: 12
Thanked 0 Times in 0 Posts
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:
To copy files which are created in particular month Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-18-2017 at 01:10 AM.. Reason: Added CODE tags.
    #11  
Old Unix and Linux 07-18-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 September 2017, 11:01 AM EDT
Location: Aachen, Germany
Posts: 11,379
Thanks: 299
Thanked 3,529 Times in 3,246 Posts
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?
Sponsored Links
    #12  
Old Unix and Linux 07-18-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 26 September 2017, 9:43 AM EDT
Location: Lancashire, UK
Posts: 3,263
Thanks: 1,394
Thanked 631 Times in 570 Posts
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
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 04:03 PM.