[comp.unix.programmers] Shell Script needed

zag@oddjob.uchicago.edu (Zack Gardner) (01/29/91)

Hello all. This question should be a breeze for all you unix experts
out there.

I need a shell script to do the following: I have a bunch of files all
named fort.11, fort.12, fort.13, etc. (not necessarily in an unbroken
sequence), and I want a command where I can say something like:

rename cn90

which will rename fort.11 to cn90.11, fort.12 to cn90.12 etc.
In other words all the fort.* files get renamed to cn90.*.

This seems tremendously simple, but I can't figure out how to do
it. Any ideas?

Thanks in advance,
Zack

frechett@spot.Colorado.EDU (-=Runaway Daemon=-) (01/29/91)

In article <1991Jan28.194439.29690@midway.uchicago.edu> zag@oddjob.uchicago.edu (Zack Gardner) writes:
>In other words all the fort.* files get renamed to cn90.*.
>Thanks in advance,
>Zack

A copy of the FAQ list has been mailed to him.  Lets not all rush out and
bombard him.. ;)

	ian

--

-=Runaway Daemon=-

jerry@ora.com (Jerry Peek) (01/29/91)

In article <1991Jan28.194439.29690@midway.uchicago.edu> zag@oddjob.uchicago.edu (Zack Gardner) writes:
> I need a shell script to do the following: I have a bunch of files all
> named fort.11, fort.12, fort.13, etc. (not necessarily in an unbroken
> sequence), and I want a command where I can say something like:
> 
> rename cn90
> 
> which will rename fort.11 to cn90.11, fort.12 to cn90.12 etc.
> In other words all the fort.* files get renamed to cn90.*.

That's not hard to do with a shell script.  I've got one called "mvm"
that does what you want.  I'll send it to anyone who wants it.

I've also got a (maybe outdated by now?) C program called "ren" that
I like a lot.  It checks for name conflicts before it renames anything.
It's a lot faster and more flexible than my shell script.  I'd also be
glad to shar the sources and send them to anyone... though there may
be a more recent version somewhere else.  Here's part of the man page:

	REN(1)                   USER COMMANDS                     REN(1)

	NAME
	     ren - rename multiple files

	SYNOPSIS
	     ren [ -d | -k | -a ] [ -v ] [path/]search replacement

	DESCRIPTION
	     Ren renames each file in the current directory  (or  in  the
	     path  directory,  if specified) that matches the search pat-
	     tern; each matching file's new name is given by the replace-
	     ment pattern.  The multiple rename is performed safely, i.e.
	     without any unexpected deletion of files due  to  collisions
	     of  replacement  names  with  existing  names, or with other
	     replacement names.  Also, all error checking is  done  prior
	     to  doing  any  renames,  so either all renames are done, or
	     none.

	     The search pattern is a filename  with  embedded  wildcards,
	     i.e.  *  and  ?, which have their usual meanings of, respec-
	     tively, matching any string of characters, and matching  any
	     single   character.   The  replacement  pattern  is  another
	     filename with embedded wildcard indexes, each of which  con-
	     sists  of  the  character # followed by a digit from 1 to 9.
	     In the new name of a matching file, the wildcard indexes are
	     replaced  by  the  actual characters that matched the refer-
	     enced wildcards in the  original  filename.   Thus,  if  the
	     search  pattern is "abc*.*.*" and the replacement pattern is
	     "xyz#1.#3",  then  "abc.txt.doc"  is  renamed  to  "xyz.doc"
	     (since the first * matched "", the second matched "txt", and
	     the third matched "doc").
		...
	     Ren safely  performs  chain  renames,  i.e.  when  file1  is
	     renamed  to  file2,  file2 to file3, file3 to file4, etc, by
	     doing the renames in the proper order.  In the case that the
	     chain  is  a cycle, i.e. filen is renamed back to file1, ren
	     breaks the cycle by using a temporary name.
		...
	AUTHOR
	     Vladimir Lanin
	     lanin@csd2.nyu.edu

--Jerry Peek, jerry@ora.com