"rename" shell command

Matthew Seaman m.seaman at infracaninophile.co.uk
Mon Jan 26 03:34:50 PST 2004


On Mon, Jan 26, 2004 at 12:03:23PM +0100, Andrew Kotsopoulos wrote:

> I'm looking for the "rename" shell command for the macosx version of 
> bsd.  In redhat and possibly other linux distributions the command 
> renames files and supports wildcards and multiple file conversions, as 
> you most likely know.  To be more precise here is the man page:

Interesting.  Sounds like it would make a good port.  However, check
out the misc/mmv port -- it sounds like it has very similar
functionality to the rename command you've been using.

Anyhow, it's a pretty trivial script to write.  Here's a perl verion I
cooked up in a few minutes -- use with care as it has only had minimal
testing.  Note too that the 'from' expression is actually a perl
regular expression, rather than a literal match.

#!/usr/bin/perl -w

$0 =~ s at .*/@@;

if (@ARGV < 3) {
	die <<E_O_USAGE;
$0: Usage:
    $0 from to file...
E_O_USAGE
}		   

$::from = shift @ARGV;
$::to   = shift @ARGV;

foreach my $file (@ARGV) {
	my $nfile;

	($nfile = $file) =~ s/$::from/$::to/;

	if ($nfile ne $file) {
		rename $file, $nfile
			or die "$0: Couldn't rename \"$file\" to \"$nfile\" -- $!\n";
	}
}
#
# That's All Folks!
#

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey         Marlow
Tel: +44 1628 476614                                  Bucks., SL7 1TH UK
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20040126/7f4c8b6a/attachment.bin


More information about the freebsd-questions mailing list