Subject: Re: renaming a group of files
To: Herb Peyerl <hpeyerl@beer.org>
From: Andrew Brown <atatat@atatdot.net>
List: netbsd-help
Date: 12/27/2002 09:29:52
> > file1.txt -> TheTestFile1.txt
> > file2.txt -> TheTestFile2.txt
> > file3.txt -> TheTestFile3.txt
> > 
> > I keep running into this type of thing.  I have a feeling that
> > there is a simple way to do it with a bit of shell
> > programming, but I am clueless.  Does anyone have any
> > thoughts?
>
>I'm sure there's a better way, but I'm never in the mood to look
>for it, so I usually do:
>
>foreach I (file?.txt)
>	mv $I `echo $I | sed -e 's/file/TheTestFile/'`
>end
>
>at the command line, and then just get on with my life.
>
>Undoubtedly you will find as many different ways to do that as there
>are atoms in the universe.

that's where perl comes in.  i have this little script that i've been
dragging around forever, that i trimmed from the original programming
perl book (pp 312-313, pub 1991).

    #!/usr/local/bin/perl
    $op = shift;
    for (@ARGV) {
	$was = $_;
	eval $op;
	die $@ if $@;
	rename($was,$_) unless $was eq $_;
    }

then i do stuff like:

    % rename 's/\.orig$//' *.orig
    % rename 'y/A-Z/a-z/ unless /^Make/' *
    % rename '$_ .= ".bad"' *.f
    % rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

or in this case:

    % rename s/file/TheTestFile/ file?.txt

-- 
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org             * "ah!  i see you have the internet
twofsonet@graffiti.com (Andrew Brown)                that goes *ping*!"
werdna@squooshy.com       * "information is power -- share the wealth."