Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign Previous Days Date As File Extension 2

Status
Not open for further replies.

lambic

Technical User
Nov 28, 2002
68
GB
Hi All,

This is probably very simple, but how do I rename a file in a shell script so that it has the previous day's date as the file extension?

E.g.

myfile.txt

Needs to be renamed...

myfile.140303

(Assuming the script was run on the 15/03/03!)

Many Thanks
 
YESTERDAY=`echo ' ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst) = localtime(time() - 86400); printf "%02d%02d%02d", $year-=100, $mon++, $mday; '|perl`
MYFILE="myfile.txt"
MYSTUB=`echo $MYFILE|cut -d'.' -f1`
mv $MYFILE $MYSTUB.$YESTERDAY

Tested OK...

mv myfile.txt myfile.030316

...note that the date is the more useful format "YYMMDD".
 
Whoops! Previous post should read...

YESTERDAY=`echo ' ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst) = localtime(time() - 86400); printf "%02d%02d%02d", $year-100, $mon+1, $mday; '|perl`
MYFILE="myfile.txt"
MYSTUB=`echo $MYFILE|cut -d'.' -f1`
mv $MYFILE $MYSTUB.$YESTERDAY`
 
I cheat by running a cron every night at 23:58 that echo's the date into a yesterday.dat file I then can use this file the next day for all sorts of things like pulling over yesterdays log files from another server ....

heres what I use ..

#!/bin/sh
# Filename: tok.sh
# This is a simple script to get the formatted date
# YYYYMMDD into a file that can be used for yesterdays date
# the following day
#######
#
# Tarn 03-04-02
# Used in & error] log collection
#############
/bin/date '+%Y%m%d' > /yesterday.dat

++++++++++++++++++++++++++++++++++++
more yesterday.dat
20030316
++++++++++++++++++++++++++++++++++++
Use it like so .. for FILE in /var/log/weblog.`cat yesterday.dat` ; do scp $FILE someone@somewhere:/tmp ; done

Or
In your case
%>mv myfile.txt myfile.`cat yesterday.dat`

Enjoy.
 
Thankyou both v.much. Both ideas work a treat!
 
tarn, you get a star for thinking outside the box. Too many people forget that you can achive what you want without having to do exactly what you are thinking. Those are usually the best solutions. Einstein47
("Vision without action is a daydream - Action without vision is a nightmare. Japanese Proverb")
 
If you have GNU's version of date...

date -d "yesterday" +"%Y......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top