LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Moving files from one directory to another based on 2 date variables (https://www.linuxquestions.org/questions/linux-newbie-8/moving-files-from-one-directory-to-another-based-on-2-date-variables-4175413185/)

dsfreddie 06-24-2012 08:51 PM

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,

Quote:

Date_Current = 20120620
Date_Previous = 20120610
Source Directory : /iis_data/source
Target Directory : /iis_data/destination
Files available in Directory:

Quote:

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 the destination Folder.
Also, I need to write the above dates to a dummy file. (so that the next job can identify the dates/files it should process)

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

Thanks Much
Freddie

chrism01 06-24-2012 09:12 PM

Files move
Code:

mv 2012061*_Employee.txt 2012062*_Employee.txt target_dir
The 2nd part is unclear; where are you getting the dates from?
For example you could write
Code:

echo "Date_Current = 20120620" >new_file
echo "Date_Previous = 20120610 >>new_file

Note '>' (overwrite) vs '>>' (append).

You should bookmark & read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

dsfreddie 06-24-2012 10:45 PM

Thanks Chris for your quick reply.

There is a file named AdhocRun.txt which will contain the following dates,

P_BUS_DATE - the latest date
P_SUB_DATE - The date until which we need to pull the files from. ie, lets say,

P_BUS_DATE = 20120620
P_SUB_DATE = 20120610

These dates will change as & when there is an adhoc processing required. Based on these adhoc dates, we should move the files from Source to destination directory.

Hope this is clearer now.

Thanks,
Freddie

montel 06-26-2012 09:43 AM

You can use the date command to give you back the current date, or modify it to give you the dates you want. For example, this will give you todays date:
Code:

$(date +"%Y%m%d")
While this will give you yesterday:
Code:

$(date -d "-1 day" +"%Y%m%d")
If you use those in a bash script, you can do what you need. Since I am not sure of which files you do not want in the future, to start, here is something basic.

Code:

#!/bin/bash
now=$(date +"%Y%m%d")
source=/iis_data/source
destination=/iis_data/destination
previous=`cat new_file | grep -o $now_Employee.txt`

if [ -z $previous ]
then
mv $source/$now_Employee.txt $destination/$now_Employee.txt
echo $source/$now_Employee >> new_file
else
echo "This has already been done"
fi

This will work for todays date only, but can be modified to what you need to do.

EDIT:
To loop through the last 30 days, add in a for loop:

Code:


for (( x=0 ; x < 30 ; x++ ))
do

d=`date -d "-$x day ago" +%Y-%m-%d`
#do the above here in place of now
mv $source/$d_Employee.txt $destination/$d_Employee.txt
done


dsfreddie 06-28-2012 11:14 AM

Hi All,

I was able to accomplish the requirement of moving the files from one directory to another using the code below,

Quote:

touch --date "20120628" /tmp/start
touch --date "20120612" /tmp/end

find /dev_data/dev3/ctl/*_Employee.txt -type f -newer /tmp/start -not -newer /tmp/end > /dev_data/dev3/tmp/output.txt

for name in `cat /dev_data/dev3/tmp/output.txt
`
do
mv $name /dev_data/dev3/inbox/
echo $name;
done
Can anybody tell whether this is a right approach ? any issues in using this code ?

Also, the 2nd requirement as I specified was to write the dates from the filenames into a .done file.
Below is how the filenames in output.txt look like

Quote:

/dev_data/dev3/ctl/20120622_Employee.txt
/dev_data/dev3/ctl/20120623_Employee.txt
/dev_data/dev3/ctl/20120624_Employee.txt
Can you pls tell me how to get only the dates from this to the .done file delimited by comma ?


All times are GMT -5. The time now is 04:38 AM.