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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-06-2012, 11:21 AM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 11
Rep:
|
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
|
|
|
06-06-2012, 11:27 AM
|
#2
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
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:
Hope this helps,
Last edited by Snark1994; 06-07-2012 at 07:40 AM.
|
|
|
06-06-2012, 11:59 AM
|
#3
|
LQ Newbie
Registered: Jun 2012
Posts: 11
Original Poster
Rep:
|
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.
|
|
|
06-06-2012, 08:05 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
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.
|
|
|
06-07-2012, 07:43 AM
|
#5
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
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.
|
|
|
06-08-2012, 03:38 PM
|
#6
|
LQ Newbie
Registered: Jun 2012
Posts: 11
Original Poster
Rep:
|
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
|
|
|
06-09-2012, 06:43 AM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
06-09-2012, 07:03 AM
|
#8
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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
|
|
|
06-09-2012, 09:59 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
@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).
|
|
|
06-10-2012, 11:52 AM
|
#10
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
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?
|
|
|
All times are GMT -5. The time now is 07:12 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|