Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: help with 'rename' function



Timothy J Luoma wrote:
> I used to have a binary that did this, but I've lost it.
> 
> What it did was simple: it changed extensions of filenames.

It's perennially annoying UNIX doesn't have this.  On the other hand,
if it did it would probably be so inscrutable as to be almost useless.

If you want a proper programme, search the archives for mmv.  There's
also a widely distributed perl script called rename which allows perl
operations on the file name, e.g. 's/\.this$/.that/' in this case.
(I can mail my version.)

If you want a shell function, it's appended...

> For example, say you have a bunch of files which ended with .THIS and you  
> wanted to change them to end with .THAT
> 
> You would do
> 
> 	rename *.THIS THIS=THAT

...the difference is it was easier to stick the THIS=THAT argument
first rather than last.  Everything's done by the shell, so it's
a bit simpler.  It's limited to suffixes as written.  Note the -f
if you want to overwrite an existing file.


# rename.zsh
local force
[[ $1 = -f ]] && shift && force=1

if [[ $# -lt 2 || $1 != *=* ]]; then
  print "Usage:  $0 oldsuf=newsuf file ..." 2>&1
fi

local old new ofile nfile
old=${1%=*}
new=${1#*=}

shift

for ofile in $*; do
  nfile=${ofile%$old}$new
  if [[ -f $nfile && $force != 1 ]]; then
    print "$nfile already exists" 2>&1
  else
    mv $ofile $nfile
  fi
done
# end

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +39 50 844536
WWW:  http://www.ifh.de/~pws/
Gruppo Teorico, Dipartimento di Fisica
Piazza Torricelli 2, 56100 Pisa, Italy



Messages sorted by: Reverse Date, Date, Thread, Author