[Malvern] Another Shell Scripting Q

Andy Lowton andy at dragonfly.demon.co.uk
Mon Dec 6 12:47:35 GMT 2004


On Fri, 2004-12-03 at 22:43 +0000, Robin Wilson wrote:
> This is another thing I'd like to do with shell scripting. I have lots
> of files named like this; filename dd mm yyyy and I'd like to rename
> them all to filename_dd-mm-yyyy. Is this possible to do quite easily
> for all files in a folder?

It is possible but i'm not sure what you mean. Do you mean the filename
has spaces like "filename dd mm yyyy"?

> Also, I have files named like this; filename_yyyy-mm-dd. Can I chgange
> those to filename_dd-mm-yyyy.?

I hate bash, but if you have python on your system try this:


#!/usr/bin/python

import os, re

dir = "."

for file in os.listdir(dir):

  m = re.search("(.+)_(\d\d\d\d)-(\d\d)-(\d\d)$", file) 

  if m:

     prefix = m.group(1)
     year = m.group(2)
     month =m.group(3)
     day = m.group(4)
     
     newfile = prefix + "_" + day + "-" + month + "-" + year
     
     print "Renaming file: " + file + " to " + newfile

     os.rename(file, newfile)

  else:

     print "Ignoring file: " + file


Cheers

Andy




More information about the Malvern mailing list