[X-Unix] Help batch renaming files with two "extentions"

Jeff Winchester jeffw at tampabay.rr.com
Wed Mar 23 19:40:21 PST 2005


On Mar 23, 2005, at 8:33 PM, David Haines wrote:

> I'm faced with a large number of files named:
> filename.w01.tif
>
> and need to be able to strip all of them of :   .tif
>
> This is certainly beyond my limited-but-slowly-growing shell-scripting
> abilities.
>
> If you suggest something perl-based, the machine in question is running
> 10.3.8, for what that will be worth. Some flavor of Perl v5.8.x

Since you mentioned Perl, I cooked up a quick-and-dirty Perl script to 
accomplish your task. I'm sure it could be improved in a number of 
ways, mainly because it's been about 6 years since I've used Perl 
daily, but here it is. At any rate, Larry Wall probably isn't 
monitoring this group. ;)

Basically, put this into a file, set permissions so it's executable, 
then call it, passing in the extension you want to strip off. So if I 
called it strip_extension, I might do

     strip_extension tif

and it would remove the trailing ".tif" from each file matching that 
criteria. It works out of the current directory (wherever you are when 
you execute it), so I'd suggest putting it in your path someplace or 
putting the script in the same location as your files to be renamed. I 
have ~/bin in my path, so that's where I put mine.


-----
#!/usr/bin/perl

use strict;

my $ext;
my $file;
my $oldfile;
my @files;

if(@ARGV) {
     $ext = $ARGV[0];

     @files = <*.$ext>;
     foreach $file (@files) {
         $oldfile = $file;
         $file =~ s/(.*)\.$ext/\1/;
         rename $oldfile, $file;
     }
}
else {
     die "ERROR: No extension provided\n";
}
-----

Good luck!


-- 
Jeff Winchester
jeffw at tampabay.rr.com



More information about the X-Unix mailing list