Script to copy file with yesterdays date and move and rename to new location

I know this is probably pretty simple but I am pounding my head on how to get this to work.

I have a directory /usr/local/myapp/funtoplay/exports that contains several output files that look like so:
MYAPP-EXPORTS-20130129.TXT
MYAPP-EXPORTS-20130130.TXT
MYAPP-EXPORTS-20130131.TXT
MYAPP-EXPORTS-20130201.TXT
MYAPP-EXPORTS-20130202.TXT
MYAPP-EXPORTS-20130203.TXT
MYAPP-EXPORTS-20130204.TXT
and so on
MYAPP-EXPORTS-20130506.TXT
MYAPP-EXPORTS-20130507.TXT

What I am trying to accomplish is take the file with yesterday’s date, in the above example the MYAPP-EXPORTS-20130506.TXT and copy it to /usr/local/myapp/funtoplay/exports/daily and then rename it to MYAPP-EXPORTS.TXT for another application to pick up and store.

I have the copy, move and rename part down but for the life of me I cannot figure out how to do this to the file which contains yesterday’s date only.

What am I missing?

I was going down something like this: /usr/local/myapp/funtoplay/exports -type f -mtime +1 -exec mv {} ;

Hi
What about something like;


myday=`date --date='1 day ago' '+%Y%m%d'`
echo $myday
cp MYAPP-EXPORTS-$myday.TXT daily/MYAPP-EXPORTS.TXT

I knew it would be easy…at least for someone who knows what they are doing.

malcolmlewis from my testing this works wonderfully!

Only issue I have found is if there is no file with yesterday’s date. I am thinking weekends where nothing happens. What is the easiest/best way to overcome that hurdle?

On Tue 07 May 2013 05:16:05 PM CDT, D8TA wrote:

I knew it would be easy…at least for someone who knows what they are
doing.

malcolmlewis from my testing this works wonderfully!

Only issue I have found is if there is no file with yesterday’s date. I
am thinking weekends where nothing happens. What is the easiest/best way
to overcome that hurdle?

Hi
Something like this then?


dow=`date +%u`

if  "$dow" = "1" ]; then
echo "It's a Monday"
myday=`date --date='3 days ago' '+%Y%m%d'`
else
echo "It's not a Monday"
myday=`date --date='1 day ago' '+%Y%m%d'`
fi

echo $myday

cp MYAPP-EXPORTS-$myday.TXT daily/MYAPP-EXPORTS.TXT


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.3 (x86_64) Kernel 3.7.10-1.4-desktop
up 16:54, 3 users, load average: 0.21, 0.13, 0.08
CPU Intel® i5 CPU M520@2.40GHz | GPU Intel® Arrandale

>


> dow=`date +%u`
>
> if  "$dow" = "1" ]; then
>    echo "It's a Monday"
>    myday=`date --date='3 days ago' '+%Y%m%d'`
> else
>    echo "It's not a Monday"
>    myday=`date --date='1 day ago' '+%Y%m%d'`
> fi
>
> echo $myday
>
> cp MYAPP-EXPORTS-$myday.TXT daily/MYAPP-EXPORTS.TXT
> 

What about the Saturday file? :wink:

I think more information may be helpful… what ideally should happen on
Monday? Pulling Friday’s file is great, but was Friday’s file the one
written all weekend (in case somebody caused something to create those
weekend files) and if not, what should happen? malcolmlewis’s solution
will work as long as you do not mind losing the weekend’s files (if they
ever exist) and as long as you really want Friday’s data only on Monday.

Good luck.

On 2013-05-07 15:46, D8TA wrote:
> What I am trying to accomplish is take the file with yesterday’s date,
> in the above example the MYAPP-EXPORTS-20130506.TXT and copy it to

Do you want the files timestamped yesterday, or the files that have
yesterday date in the name?

There is a big difference.

Also, please, use code tags to post scripts and other things from the
terminal. Advanced editor, ‘#’ button.
Posting in
Code Tags - A Guide


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)