LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-06-2012, 11:21 AM   #1
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
Move the files between Current day & a previous day


Hi All,

I have a requirement where I need to first capture the current day & move all the files from a particular directory between the current day & a previous day. (BUS_DATE)

Here is what I am trying, but it gives me errors. ( I think those are syntax errors)

Quote:
export BUS_DATE=`cat path/filename.txt| grep 'P_BUS_DATE' | cut -d"=" -f2`
CURR_DY=`date "+%Y%m%d"`
CURR_DY_SECS=`date --date=$CURR_DY +"%s"`
BUS_DATE_SECS=`date --date=$BUS_DATE +"%s"`
DAYS=`$((($CURR_DY_SECS - $BUS_DATE_SECS) / 86400 ))`
echo $DAYS
i=$DAYS
while [ $i -ge 0 ]
do
`mv /patha/filename.$(expr $CURR_DY - $i).dat /pathb/destination/`
i=$ [ expr $i - 1 ]
done
I am also attaching the errors I got,

Quote:
++ grep P_BUS_DATE
++ cut -d= -f2
+ export BUS_DATE=20120528
+ BUS_DATE=20120528
++ date +%Y%m%d
+ CURR_DY=20120605
++ date --date=20120605 +%s
+ CURR_DY_SECS=1338872400
++ date --date=20120528 +%s
+ BUS_DATE_SECS=1338181200
++ 8
script.ksh: line 31: 8: command not found
+ DAYS=
+ i=
+ '$DAYS'
scripts.ksh: line 33: $DAYS: command not found
+ '[' -ge 0 ']'
scripts.ksh: line 34: [: -ge: unary operator expected
Can you pls shed some light on this.

Thanks much in advance,
Freddie
 
Old 06-06-2012, 11:27 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You don't want the backticks in the DAYS= line:

Code:
DAYS=$((($CURR_DY_SECS - $BUS_DATE_SECS) / 86400 ))
Same goes for the 'mv' line:

Code:
mv /patha/filename.$(( $CURR_DY - $i)).dat /pathb/destination/
and the "i=" line at the bottom could be better written as:

Code:
i=$(($i - 1))
Hope this helps,

Last edited by Snark1994; 06-07-2012 at 07:40 AM.
 
Old 06-06-2012, 11:59 AM   #3
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks Snark for your quick response :-)

I made the changes you mentioned. Here is the error I am getting,

Quote:
++ grep P_BUS_DATE
++ cut -d= -f2
+ export BUS_DATE=20120528
+ BUS_DATE=20120528
++ date +%Y%m%d
+ CURR_DY=20120606
++ date --date=20120606 +%s
+ CURR_DY_SECS=1338958800
++ date --date=20120528 +%s
+ BUS_DATE_SECS=1338181200
+ DAYS=9
+ i=9
+ [[ 9 -ge 0 ]]
++ expr 20120606 - 9
+ mv /path/filename.20120597.dat /iis_dev_data/wcc/cpmg/ctl/
mv: cannot stat `/path/filename.20120597.dat': No such file or directory
+ i=8
+ [[ 8 -ge 0 ]]
++ expr 20120606 - 8
+ mv //path/filename.20120598.dat /iis_dev_data/wcc/cpmg/ctl/
mv: cannot stat `/path/filename.20120598.dat': No such file or directory
+ i=7
+ [[ 7 -ge 0 ]]
++ expr 20120606 - 7
This error continues until loop is over.

Can you pls help
Freddie

Last edited by dsfreddie; 06-06-2012 at 12:26 PM.
 
Old 06-06-2012, 08:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
You're getting those because
Code:
++ expr 20120606 - 9
+ mv /path/filename.20120597.dat /iis_dev_data/wcc/cpmg/ctl/
(as far as bash is concerned) you are subtracting integers, NOT dates.
 
Old 06-07-2012, 07:43 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Like chrism01 said, if you wanted to work out the correct date, you could use 'date' again:

Code:
mv /patha/filename.$(date --date=@$(($CURR_DY_SECS - $i*24*60*60)) +%Y%m%d).dat /pathb/destination/
(I notice in your examples you use "date --date=123456789" not "date --date=@123456789". If your version of 'date' doesn't need the @, then just remove it from my code)

You're using date again, feeding it the current date in seconds, but subtracting $i*24*60*60 seconds (which should be $i days' worth of seconds) and then outputting the corresponding date.
 
Old 06-08-2012, 03:38 PM   #6
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Reading the dates from a file & moving the files from a directory

Hi All,

I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another.

ie, this is how it will be in the file,

SUB_DATE = 20120608,20120607,20120606,20120606

Now, i need to move the file (with filename like a.20120608,a.20120607 etc) based on the above dates from /path/source to /path/destination.

Can anybody shed some light on how to achieve this in LINUX?

Thanks Much
Freddie
 
Old 06-09-2012, 06:43 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Could you clarify it a bit more? What are the exact contents of the file? Is it just that line, or are there others? Please post a representative example.

And does each date in the SUB_DATE string match an exact filename? Could there be dates without files, or files without dates, and what should happen in such cases? Please post a sample of the directory tree as well.

In any case, this should all be doable with a simple shell script. A while+read loop, some simple string manipulation, and mv are probably all that's required. Here are some links to start you with:

http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ/001
http://mywiki.wooledge.org/BashFAQ/100
 
1 members found this post helpful.
Old 06-09-2012, 07:03 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Here's a start:

"filename" contains one date string---eg "20120908":

Code:
dat=`grep -o "20[0-9]*" filename`
cp filename filename_$dat
Note the "backtics"---not single quotes
 
Old 06-09-2012, 09:59 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@dsfreddie: moderator note: your new thread has been merged here, since the new question is strictly related to the previous one. Furthermore, keeping discussions in one place avoids confusion and gives your question a better exposure (in respect of members that already stepped in to help).
 
Old 06-10-2012, 11:52 AM   #10
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
This is beginning to sound a little like an X-Y Problem... What is your actual end goal? What problem are you trying to solve?
 
  


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
how to check logs for previous day mohdsuboor23 Linux - Server 10 10-07-2010 12:22 PM
Show files not directories than have been modified within the current day (NEWTHR) jianelisj Linux - Newbie 2 03-18-2008 05:58 PM
Show all files not directories than have been modified within the current day jianelisj Linux - Newbie 2 03-08-2008 05:54 PM
Can you restore a cron from previous day? Rustylinux SUSE / openSUSE 1 01-15-2007 06:07 PM
A Long Day - Slackware-Current & 2.6.4 Installation Disaster SML Slackware 1 03-21-2004 06:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:12 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
Open Source Consulting | Domain Registration