#!/usr/bin/perl -w # vim: set sw=4 ts=4 si et: # my $status = 0; my $expr; ($expr = shift) || &help; &help if ($expr eq "-h"); &help unless (@ARGV); my @ltime = localtime; my $date=sprintf("%02d%02d%02d",1900 + $ltime[5] - 2000,$ltime[4] + 1,$ltime[3]); for (@ARGV) { $was = $_; eval $expr; die $@ if $@; if ($was ne $_) { if (-e "$_" ){ print STDERR "ERROR: file $_ does already exist\n"; $status = 1; next; } unless (rename($was, $_)) { print STDERR "$0: can't rename $was to $_: $!\n"; $status = 1; } } } exit $status; #---------------------------------------------------------- sub help{ print "Usage: rename [-h] perlexpr files \"rename\" provides the filename in \$_ to perlexpr and renames according to the new value of \$_ modified by perlexpr. You can use the special variable \$date to get the current date. EXAMPLE: -Lower case all *.TXT files: rename \'tr/A-Z/a-z/\' *.TXT -Change ending .HTM to .html: rename \'s/\\.htm\$/.html/i\' *.HTM -add a date at the beginning: rename \'s/^/\$date/\' * \n"; exit; } #---------------------------------------------------------- __END__ .TH RENAME 1 "July 30, 1990" .AT 3 .SH NAME rename \- renames multiple files .SH SYNOPSIS .B rename [-ih] perlexpr files .SH DESCRIPTION .I Rename renames the filenames supplied according to the rule specified as the first argument. The argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. .PP .PP For example, to rename all files matching *.bak to strip the extension, you might say .nf rename 's/\e.bak$//' *.bak .fi To translate uppercase names to lower, you'd use .nf rename 'y/A-Z/a-z/' * .fi To do the same thing but leave Makefiles unharmed: .nf rename 'y/A-Z/a-z/ unless /^Make/' * .fi To rename all the *.f files to *.BAD, you'd use .nf rename 's/\e.f$/.BAD/' *.f .SH ENVIRONMENT .fi No environment variables are used. .SH AUTHOR Larry Wall .SH "SEE ALSO" mv(1) .br perl(1) .SH DIAGNOSTICS If you give an invalid Perl expression you'll get a syntax error. .SH BUGS .ex