Duelling shells (Re: shell architecture (to glob or not to glob))

Tom Christiansen tchrist at convex.COM
Wed Mar 20 00:41:14 AEST 1991


>From the keyboard of peter at ficc.ferranti.com (Peter da Silva):
:In article <669304840 at juliet.cs.duke.edu> drh at duke.cs.duke.edu (D. Richard Hipp) writes:
:> In article <1407 at sheol.UUCP> throopw at sheol.UUCP (Wayne Throop) writes:
:> >...[R]enaming groups of files is a common
:> >enough task
:
:Is it? When was the last time you did this, other than moving files from
:one directory to another?
:
:> >that *some* tool (whather mv or not) ought to be around to
:> >handle it.
:
:> ls *.x | sed -e 's/\.x//' | while read name; do mv $name.x $name.y; done;
:
:ls *.x | sed 's/\(.*\).x/mv & \1.y/' | sh

from the FAQ...  try rename:

    $ rename 's/\.x$/.y/' *.x

program and man page follow.  link /usr/local/bin/rename to
/usr/local/man/man1/rename.1; the code was orginally much shorter before i
added the -i flag (like mv's) to lwall's initial code.

--tom

#!/usr/bin/perl
'di';
'ig00';
#
# $Header: rename,v 3.0.1.2 90/08/09 03:17:57 lwall Locked $
#
# $Log:	rename,v $
# Revision 3.0.1.2  90/08/09  03:17:57  lwall
# patch19: added man page for relink and rename
# 

if ($ARGV[0] eq '-i') {
    shift;
    if (open(TTYIN, "</dev/tty") && open(TTYOUT,">/dev/tty")) {
	$inspect++;
	select((select(TTYOUT),$|=1)[0]);
    } 
}
($op = shift) || die "Usage: rename [-i] perlexpr [filenames]\n";
if (!@ARGV) {
    @ARGV = <STDIN>;
    chop(@ARGV);
}
for (@ARGV) {
    unless (-e) {
	print STDERR "$0: $_: $!\n";
	$status = 1;
	next;
    } 
    $was = $_;
    eval $op;
    die $@ if $@;
    if ($was ne $_) {
	if ($inspect && -e) {
	    print TTYOUT "remove $_? ";
	    next unless <TTYIN> =~ /^y/i;
	} 
	unless (rename($was, $_)) {
	    print STDERR "$0: can't rename $was to $_: $!\n";
	    $status = 1;
	}
    } 
}
exit $status;
##############################################################################

	# These next few lines are legal in both Perl and nroff.

.00;			# finish .ig
 
'di			\" finish diversion--previous line must be blank
.nr nl 0-1		\" fake up transition to first page again
.nr % 0			\" start at page 1
';<<'.ex'; #__END__ ############# From here on it's a standard manual page ############
.TH RENAME 1 "July 30, 1990"
.AT 3
.SH NAME
rename \- renames multiple files
.SH SYNOPSIS
.B rename [-i] 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.
If no filenames are given on the command line, filenames will be read
via standard input.
.PP
The 
.B \-i
flag will prompt to remove the old file first if it exists.  This
flag will be ignored if there is no tty.
.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 FILES
.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
.I Rename
does not check for the existence of target filenames, so use with care.
.ex



More information about the Comp.unix.shell mailing list