LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2012, 09:51 PM   #1
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
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
 
Old 06-24-2012, 10:12 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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/
 
Old 06-24-2012, 11:45 PM   #3
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-26-2012, 10:43 AM   #4
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
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

Last edited by montel; 06-26-2012 at 10:55 AM. Reason: add content that should have been there before
 
Old 06-28-2012, 12:14 PM   #5
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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 ?

Last edited by dsfreddie; 06-28-2012 at 12:16 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy all the files in the directory based on the modification date SriniKlr Programming 4 01-26-2011 11:08 AM
Variables in moving / renaming files using CURL to download Freaksta Linux - Software 3 08-30-2010 10:13 AM
Sort files in directories based on the files date... CharlieMike73 Programming 5 09-09-2009 11:04 PM
selectively tar based on date just the innermost directories of a single directory rsiazon Linux - Newbie 5 07-29-2009 01:15 AM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 08:30 AM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration