[NBLUG/talk] perl script to rename file with a julian date

Eric Eisenhart eric at nblug.org
Tue Feb 3 17:46:02 PST 2004


On Tue, Feb 03, 2004 at 03:46:10PM -0800, Dave Cooper wrote:
> Well I've been playing around, with no success...
> 
> I have 2 files in a /dir
> 
> fileA.rpt and fileB.rpt
> 
> everynight @ 2000 I need to rename the files with the julian date as the 
> extension....
> 
> Trying to stay with perl...
> 
> my work looks and is very tacky, so I'll not post my attempt...

Do you mean they become fileA.rpt034, fileA.rpt.034 or fileA.034 ?

Assuming the last case, I'd use shell scripting to do it.

for filename in fileA.rpt fileB.rpt
do
  mv $filename `basename $filename .rpt`.`date +%j`
done

But, as andrew pointed out, something more readable than the Julian date is
nice.

I like `date +%Y%m%d%H%M%S` (you can throw dashes or colons or whatever
before the percent signs as you feel appropriate, and subtract from the end
until you have only the resolution required for your job, or even add more
precision.  IOW, stuff like +%Y/%m/%d-%H:%M:%S.%N could be nice, too).  It's
Y2K (and Y2.1K) proof; sorts lexically, alphabetically and numerically; and
it's international (YYYYMMDD is unambiguous.  MMDDYYYY and DDMMYYYY easily
get confused because much of the rest of the world does it the other way
than we do, and it only gets worse with 2-digit years)

Seriously, is there any doubt what date "20040203" (%Y%m%d) is?  There sure
is for 030204, 020304 and 040203, and some for 02032004 vs. 03022004.
-- 
Eric Eisenhart
NBLUG Co-Founder & Director-At-Large
The North Bay Linux Users Group
http://nblug.org/
eric at nblug.org, IRC: Freiheit at freenode, AIM: falschfreiheit, ICQ: 48217244



More information about the talk mailing list