Moving files from one directory to another based on 2 date variables

Hi All,

I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below,

Date_Current = 20120620
Date_Previous = 20120610

Source Directory : /iis_data/source
Target Directory : /iis_data/destination

Files available in Directory:

20120609_Employee.txt
20120612_Employee.txt
20120615_Employee.txt
20120616_Employee.txt
20120617_Employee.txt
20120620_Employee.txt

I need to move all these files (EXCEPT the 20120609_Employee.txt) to destination Folder.
Also, I need to write the above dates from the files to a dummy file.

Can you pls help me figure out how to do it ? Any help is appreciated.

Thanks Much
Freddie

The nice thing about YYYYMMDD dates is they sort and compare alphabetically.

#!/bin/sh

 Date_Current=20120620
Date_Previous=20120610

IFS="_"
for FILE in *.txt
do
        set -- $FILE

        if [ "$1" ">" "$Date_Previous" ] && [ "$1" "<" "$Date_Current" ] ||
                [ "$1" = "$Date_Previous" ] || [ "$1" = "$Date_Current" ]
        then
                echo mv "$FILE"
        fi
done

Thanks Corona for your reply.
This is what I am trying in my sample script,

 
Date_Current=20120620
Date_Previous=20120610
IFS="_"
for EMPLOYEE in /iis_data/source/*.txt
do
set -- $FILE
if [ "$1" ">" "$Date_Previous" ] && [ "$1" "<" "$Date_Current" ] ||
[ "$1" = "$Date_Previous" ] || [ "$1" = "$Date_Current" ]
then
echo mv "$FILE /iis_data/destination/"
fi
done

Can you pls let me know what I am doing wrong here.
Error Message:

 
+ for EMPLOYEE in '/iis_data/source/*.txt'
+ set --
+ '[' '' '>' 20120610 ']'
+ '[' '' = 20120610 ']'
+ '[' '' = 20120620 ']'

That's not an error message. :confused:

for EMPLOYEE in '/iis_data/source/*.txt'

Did you put this in quotes in your original? It won't work if its in quotes.

Hi Corona,

Thanks for your reply. This is what I am seeing in the error log,

Date_Current=20120620
+ Date_Previous=20120610
+ IFS=_
/iis_data/source/20120609_EMPLOYEE.txt /iis_data/source/20120618_EMPLOYEE.txt /iis_data/source/20120619_EMPLOYEE.txt /iis_data/source/20120622_EMPLOYEE.txt /iis_data/source/CallParam.txt /iis_data/source/convertfuntionn.txt /iis_data/source/convert.txt /iis_data/source/Copybook.txt /iis_data/source/CPMGBUSDynamicDateFile1.txt /iis_data/source/CPMG_CDPH_Approach.txt /iis_data/source/CPMGDynamicDateFile_0410.txt 

Looks like it read all the files that end with *.txt. Also, doesnt seem to move the files to the destination directory.
I suspect the below code part is causing the issue,

for EMPLOYEE in `/iis_data/source/*.txt`
do
set -- $FILE
if [ "$1" ">" "$Date_Previous" ] && [ "$1" "<" "$Date_Current" ] ||
[ "$1" = "$Date_Previous" ] || [ "$1" = "$Date_Current" ]
then
echo mv "$FILE /iis_data/destination/"
fi
done

Assuming that there is only one file per date, that the only dates in filenames in that directory occur in files of interest, and that there aren't other files whose names may sort within the date range, the following may be of use:

ls | sed -n "/$Date_Previous/,/$Date_Current/p; /$Date_Current/q" | xargs -I_ echo mv _ /iis_data/destination/

If the output is acceptable, remove the word "echo" to actually move files.

Regards,
Alister

1 Like

You did it again:

for EMPLOYEE in `/iis_data/source/*.txt`

That is in backticks and should not be.

In the code I actually posted, it was in no brackets, quotes, or ticks whatsoever. It is not supposed to be in any whatsoever. It will not work if you put it in any whatsoever.

Try the code I gave you without editing things that look funny. It is working code.

1 Like

Hi Corona,

I am pasting the actual code here. Also attaching the logs. I hope this help you narrow down the cause of the issue.

The CPMGPERDynamicDateFile.txt has the following dates in it,

DYN_BUS_DATE=20120625
P_SUB_DATE=20120603

Files in /iis_dev_data3/wcc/cpmg/tmp

20120619_EMPLOYEE.txt
20120618_EMPLOYEE.txt
20120622_EMPLOYEE.txt
20120609_EMPLOYEE.txt
 
export DYN_BUS_DATE=`cat $COMMON_TMP/CPMGPERDynamicDateFile.txt| grep 'P_BUS_DATE' | cut -d"=" -f2`
export P_SUB_DATE=`cat $COMMON_TMP/CPMGPERDynamicDateFile.txt| grep 'P_SUB_DATE' | cut -d"=" -f2`
 
Date_Current=$DYN_BUS_DATE
Date_Previous=$P_SUB_DATE
IFS="_"
for EMPLOYEE in /iis_dev_data3/wcc/cpmg/tmp/*.txt
do
set -- $FILE
if [ "$1" ">" "$Date_Previous" ] && [ "$1" "<" "$Date_Current" ] ||
[ "$1" = "$Date_Previous" ] || [ "$1" = "$Date_Current" ]
then
echo mv "$FILE /iis_dev_data3/wcc/cpmg/inbox/"
fi
done

Thanks Much
Freddie

Hello Alister,

Thanks for your reply. Here is the answer for your assumptions.

1) There will be only one EMPLOYEE file per date, but there are multiple files in the same directory with dates within the P_BUS_DATE/P_SUB_DATE range
2) All the files in the directory starts with date.

In the below code, can you pls tell where we are passing the file name ?

ls | sed -n "/$Date_Previous/,/$Date_Current/p; /$Date_Current/q" | xargs -I_ echo mv _ /iis_data/destination/ 

Also, is there a way I can write the dates for which there is file availability into a .done file ? (ie, if the date range is 20120621 to 20120617, lets say there are no files for 20120619,20120620.
In this case, the .done file should only have 20120621,20120618,20120617 in it.

Thanks Much
Freddie

Forgot to change a variable name:

for EMPLOYEE in /iis_dev_data3/wcc/cpmg/tmp/*.txt
do
        set -- $EMPLOYEE
        if [ "$1" ">" "$Date_Previous" ] && [ "$1" "<" "$Date_Current" ] ||
                [ "$1" = "$Date_Previous" ] || [ "$1" = "$Date_Current" ]
        then
                echo mv "$FILE /iis_dev_data3/wcc/cpmg/inbox/"
        fi
done

Hi Corona,

I made the change you suggested.Here is what I see in logs now, (looks like soemthing is still wrong :frowning:

Thanks
Freddie

Okay, putting a path into there has changed it a lot.

set -- `basename $EMPLOYEE`