|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 0And 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
|
||||
|
||||
|
Thanks Methyl for your reply. Pls find the answers to your queries below,
LINUX version : Quote:
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:
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:
Quote:
the .done should have values as below, 20120615,20120614,20120613 Hope i answered your questions. Thanks Much for your help, Freddie |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
Besides, i have to apologize for my typo: it's maxdepth, with the h, not maxdept. Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|