Path: utzoo!attcan!uunet!cs.utexas.edu!sun-barr!newstop!texsun!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.shell Subject: Re: Rename script or ...? Message-ID: <1991Feb06.124655.4504@convex.com> Date: 6 Feb 91 12:46:55 GMT References: Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 143 Nntp-Posting-Host: pixel.convex.com From the keyboard of dag@beppe.ericsson.se: : :I am used to the VAX/VMS world. There I can do the following commands : :rename foo.* bar.* :rename *.pas *.c : :The first of these commands would rename all files beginning with 'foo.' :to 'bar.' but retaining the suffixes from the input. The second command :would only change the suffix. : :How should I do this in UNIX? Is there a tool to use or do I have to write a :script, and in that case, what should the script look like? This one is question 9 in the FAQ, guys, last posted by Steve on Monday. If you like typing in shell loops every time, feel free; personally, I'm too lazy for that. Your posting will probably trigger a dozen little C programs that people have written over the years. These have two disadvantages: they have to recompiled for each system (not too bad) and they can perform a very limited set of transformations on the filename (unacceptable). Using Larry Wall's rename script (contained in the FAQ), you can perform absolutely arbitrary changes to file names. I usually just stick what often amounts to a sed expression in there, but in fact you have the full power of perl, so there's probably not much you can't do in the way of filename munging. For example, here are your two examples in lwall's rename: :rename foo.* bar.* % rename 's/^foo\/bar/' foo.* :rename *.pas *.c % rename 's/\.pas$/.c//' *.pas There are long C programs that do this, but this is only the tip of the iceberg of the power packed into Larry's little 8-line perl script; basically, this is because it can 'eval' the expr, which means All Things Are Possible. Once you understand when globbing occurs, and are used to sed/ed/ex/vi style s/subs/titutions/, this approach makes a lot of sense. I have a slighly frillier version of rename that includes a -i switch to emulate mv's -i switch and to do a bit more error checking, which I'll mail you if you want. Here is Larry's basic version, which is also its own man page. Extract this, make it executable, and link it to both to /usr/local/bin/rename as well as /usr/local/man/man1/rename.1 -- it's one of those files that serves double duty. This version also reads from a find pipe: find foo -name blah -print | rename 'expr' --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 # ($op = shift) || die "Usage: rename perlexpr [filenames]\n"; if (!@ARGV) { @ARGV = ; chop(@ARGV); } for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; } ############################################################################## # 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 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 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 .SH ENVIRONMENT 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 -- "Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI window manager, which argues with you for 10 weeks before resizing your window." ### And now for the question of the month: How do you spell relief? Answer: U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U Brought to you by Super Global Mega Corp .com