\_ I'm trying to rename a ton of files with a certain extension to another
\_ extension type. If this is possible with the mv command I can't figure out
\_ the syntax.. Anyone know how to do this?
There's a foreach in your shell of choice that'll run something like
foreach f (*.DAT)
mv $f $f.broken
end
but that can be kinda tedious to type in from whole cloth if you don't
do it a whole bunch.
Having seen this request any number of times now, I dropped out a
quick perl script that'll do it w/ args. If it's worth doing once,
it's worth scripting for later reuse. :-)
I called it map-mv after the lisp command 'map', which rocks.
Try map-mv -h for help.
YMMV.
David
#!/usr/bin/perl
sub print_help
{
print <<EOHELP;
usage $0: [flags]
-f [x]: from extension
-t [x]: to extension
-n : no renaming is to take place
-h : this help
EOHELP
;
}
use Getopt::Std;
getopts('f:ht:n',\%opt);
if ($opt{h})
{
&print_help;
exit;
}
opendir(DIR, '.') || die "Can't open .: $!\n";
my @files = grep(/$opt{f}/, readdir(DIR));
closedir(DIR);
foreach my $file (@files)
{
($newfile = $file) =~ s/$opt{f}$/$opt{t}/;
if ($opt{'n'})
{
print "rename $file, $newfile\n";
}
else
{
if (! rename ($file, $newfile))
{
warn "Could not move $file to $newfile: $!\n";
}
}
}