Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-14-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
How to move files between 2 dates from one directory to another

Hi All,

I am coding for a requirement where I need to move files (filename.yymmdd) from one directory(A) to another(B) based on 2 date fields in a paramtere file. (Paramfile.txt)

For e.g: In Paramfile.txt,


Code:
BUS_DT =20120612
SUB_DT =20120602

In this case, i need to move all the files from directory A to directory B which falls between these 2 dates.

Also, I need to capture these dates(In a comma delimited format) into a .done file.

Can you pls shed some light on this.

Any thoughts are appreciated. Pls note i am trying to accomplish this in LINUX based system.

Thanks
Freddie

Last edited by methyl; 06-14-2012 at 05:44 PM.. Reason: please use code tags
Sponsored Links
    #2  
Old 06-14-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Location: Lombardia, Italy
Posts: 179
Thanks: 5
Thanked 38 Times in 38 Posts
Quote:
Originally Posted by dsfreddie View Post
Hi All,

I am coding for a requirement where I need to move files (filename.yymmdd) from one directory(A) to another(B) based on 2 date fields in a paramtere file. (Paramfile.txt)

For e.g: In Paramfile.txt,

BUS_DT =20120612
SUB_DT =20120602

In this case, i need to move all the files from directory A to directory B which falls between these 2 dates.
I'm not sure whether I got it or I didn't.
Could you please clarify what has to fall between the two dates?
The date in the name of the files (filename.yymmdd), or the files modification time, or...?

--
Bye
Sponsored Links
    #3  
Old 06-14-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Thanks Lem for your reply. Let me clarify your queres.

Could you please clarify what has to fall between the two dates?

The filename is as (yymmdd_filename). the date value in the filename should fall between the BUS_DT & SUB_DT values.

Hope this is clear now.

Thanks Much
Freddie
    #4  
Old 06-14-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Please post what Operating System and version you have. There are umpteen Linuxes.
Please post what Shell you use.
Please post whether you have the GNU version of the date command and whether you have Perl installed.

And to labour the point. Is your range inclusive or exclusive or some combination of the two. Please give a simple example showing real filenames in the form of an abbreviated ls -la listing (I hope your filenames do not actually have brackets in them).

Can we lose the space character in the parameter file design to match Shell syntax, so we can make the parameter file executable and import the parameters into the Shell environment?

Very important: Does the directory timestamp on every file match the date in the name of the file?
Sponsored Links
    #5  
Old 06-15-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Location: Lombardia, Italy
Posts: 179
Thanks: 5
Thanked 38 Times in 38 Posts
Untested:


Code:
#!/bin/bash

PARAMFILE="/path/to/paramfile.txt" # Please set the right absolute path
#example: PARAMFILE="/home/johh/paramfile.txt"

DIRA="/path/to/dirA" # Please set the right absolute path
#example: DIRA="/home/john/things/stuff"

DIRB="/path/to/dirB" # Please set the right absolute path
#example: DIRB="/home/john/things/otherstuff"

SINCE=$(grep SUB_DT "$PARAMFILE" 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)
TILL=$(grep BUS_DT "$PARAMFILE" 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)

find "$DIRA" -maxdepth 1 -type f | while IFS= read -r NOMEFILE; do
     FILEDATE=$(egrep -o "[0-9]{8}$" <<<"$NOMEFILE" 2>/dev/null)
     [[ $FILEDATE != "" ]] && (( $FILEDATE >= $SINCE )) && (( $FILEDATE <= $TILL )) && cp -t "$DIRB" "$NOMEFILE"
     done
exit 0

And I used cp instead of mv: files are copied, not moved, so you can test the script and prevent any damage.

It's a bit late, here. See you tomorrow for everything.
--
Bye

---------- Post updated at 10:24 AM ---------- Previous update was at 12:04 AM ----------

I realize now that I missed the last part of your request (the .done file).

So we need to put $SINCE and $TILL in this file.done, right?

Something like:

[...]
20120318,20120325
20120326,20120406
[...]

Am I right?

Last edited by Lem; 06-15-2012 at 10:33 AM.. Reason: Added some quotes, just to be safer
Sponsored Links
    #6  
Old 06-15-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Thanks Methyl for your reply. Pls find the answers to your queries below,

LINUX version :
Quote:
uname -r
2.6.18-274.el5

what Shell you use : ksh
whether you have the GNU version of the date command and whether you have Perl installed - the date command gives me an output as below,
Quote:
date
Fri Jun 15 08:49:28 CDT 2012

Is your range inclusive or exclusive or some combination of the two : Yes, if i pass the BUS_DATE=20120615 & SUB_DATE=20120611, it should even include files for these 2 dates as well.

Please give a simple example showing real filenames in the form of an abbreviated ls -la listing (I hope your filenames do not actually have brackets in them).

filename is as below,
20120615_filename.dat
20120614_filename.dat etc

Very important: Does the directory timestamp on every file match the date in the name of the file?
Need not be the same. If we run the run the process that generates the file multiple times a day, the timestamp will be the same, but the date value in the file will be different.

Hope I answered your questions.

Thanks much,
Freddie

---------- Post updated at 10:26 AM ---------- Previous update was at 09:53 AM ----------

Hi Lam,

Thanks for the solution you provided. I tried running it, but it throws error msg, Here is what I tried

Quote:
PARAMFILE=/dev/tmp/DynamicDateFile.txt
DIRA=/dev/cmg/ctl/
DIRB=/dev/cmg/ctl/inbox/

SINCE=$(grep BUS_DT $PARAMFILE 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)
TILL=$(grep SUB_DT $PARAMFILE 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)
find $DIRA -maxdept 1 -type f | while IFS= read -r filenameA; do
FILEDATE=$(egrep -o "[0-9]{8}$" <<<$filenameA 2>/dev/null)
[[ $FILEDATE != "" ]] && (( $FILEDATE >= $SINCE )) && (( $FILEDATE <= $TILL )) && cp -t $DIRB $NOMEFILE
done
exit
Error msg :

Quote:
PARAMFILE=/dev/tmp/scripts/DynamicDateFile.txt
+ DIRA=/dev/cmg/ctl/
+ DIRB=/dev/cmg/ctl/inbox/
++ grep BUS_DT //dev/tmp/scripts/DynamicDateFile.txt
++ egrep -o '[0-9]{8}$'
+ SINCE=
++ grep SUB_DT /dev/tmp/scripts/DynamicDateFile.txt
++ egrep -o '[0-9]{8}$'
+ TILL=
+ find /dev/cmg/ctl/ -maxdept 1 -type f
+ IFS=
+ read -r filename
find: invalid predicate `-maxdept'
+ exit
Also,regarding the .done file, if I pass the BUS_DATE=20120615 & SUB_DATE=20120613,
the .done should have values as below,

20120615,20120614,20120613

Hope i answered your questions.

Thanks Much for your help,
Freddie
Sponsored Links
    #7  
Old 06-15-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Location: Lombardia, Italy
Posts: 179
Thanks: 5
Thanked 38 Times in 38 Posts
Quote:
Originally Posted by dsfreddie View Post
Hi Lam,

Thanks for the solution you provided. I tried running it, but it throws error msg, Here is what I tried



Error msg :
Please, double check where your parameter file is: /dev/tmp or /dev/tmp/scripts?

Besides, i have to apologize for my typo: it's maxdepth, with the h, not maxdept.



Quote:
Also,regarding the .done file, if I pass the BUS_DATE=20120615 & SUB_DATE=20120613,
the .done should have values as below,

20120615,20120614,20120613
20120614 should be present even if no file has this value in its name?
If so, let's try with:


Code:
#!/bin/ksh

PARAMFILE="/path/to/paramfile.txt" # Please set the right absolute path
#example: PARAMFILE="/home/johh/paramfile.txt"

DIRA="/path/to/dirA" # Please set the right absolute path
#example: DIRA="/home/john/things/stuff"

DIRB="/path/to/dirB" # Please set the right absolute path
#example: DIRB="/home/john/things/otherstuff"

DONEFILE="/path/to/file.done" #Please set the righ absolute path

SINCE=$(grep SUB_DT "$PARAMFILE" 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)
TILL=$(grep BUS_DT "$PARAMFILE" 2>/dev/null | egrep -o "[0-9]{8}$" 2>/dev/null)

find "$DIRA" -maxdepth 1 -type f | while IFS= read -r NOMEFILE; do
     FILEDATE=$(egrep -o "[0-9]{8}$" <<<"$NOMEFILE" 2>/dev/null)
     [[ $FILEDATE != "" ]] && (( $FILEDATE >= $SINCE )) && (( $FILEDATE <= $TILL )) && cp -t "$DIRB" "$NOMEFILE"
     done
for z in {$SINCE..$TILL}; do
    /bin/echo -n $z"," >> "$DONEFILE"
    done
exit 0

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading the dates from a file & moving the files from a directory dsfreddie UNIX for Dummies Questions & Answers 5 06-09-2012 01:36 PM
Move files to another directory based on name suresh01_apk Shell Programming and Scripting 3 09-30-2011 04:10 AM
Move files of a certain year to other directory codenjanod Red Hat 6 04-22-2010 04:33 PM
Move all files in a directory tree to a signal directory? briandanielz UNIX for Dummies Questions & Answers 2 06-15-2008 05:20 PM



All times are GMT -4. The time now is 09:03 AM.