simple question about mv

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Thu Feb 16 09:03:43 AEST 1989


In article <228 at torch.UUCP> richard at torch.UUCP (Richard Nuttall) writes:
: DEG41560 at ua1vm.ua.edu (Rob Smith) writes:
: 
: >HI,
: 
: > I know this is simple, but then again, so am I.  What if I want to mv a bunch
: >of files with the same suffix to another suffix. The following does not
: >work
: 
: >    mv *.flip *.flop
: 
: I prefer the C shell foreach, which is really neat :
: 
: foreach i (*.flip)
: echo $i
: mv $i $i:r.flop
: end

Yes, you can use csh for that, but it's an awful lot of typing.  If you
have perl, you can put this script into your system as "rename":

#!/usr/bin/perl

$subst = shift;
@ARGV = <*> if $#ARGV < 0;

foreach $name (@ARGV) {
    $_ = $name;
    eval "$subst;";
    die $@ if $@;
    rename($name,$_) unless $name eq $_;
}


You can now say things like:

	rename 's/\.flip$/.flop/'	# rename *.flip to *.flop
	rename s/flip/flop/		# rename *flip* to *flop*
	rename 's/^s\.(.*)/$1.X/'	# switch sccs filenames around
	rename 's/$/.orig/ */*.[ch]'	# add .orig to your source files in */
	rename 'y/A-Z/a-z/'		# lowercase all filenames in .
	rename 'y/A-Z/a-z/ if -B'	# same, but just binaries!

    or even

	rename chop *~			# restore all ~ backup files

The script could of course be modified to be verbose, or to confirm, or
to get filenames from find, or to not wipe out existing files.  For that
matter, you could probably modify it into a C compiler if you tried hard
enough.  :-)

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.wizards mailing list