Shell Script needed
Jerry Peek
jerry at ora.com
Tue Jan 29 22:42:12 AEST 1991
In article <1991Jan28.194439.29690 at midway.uchicago.edu> zag at 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 at csd2.nyu.edu
--Jerry Peek, jerry at ora.com
More information about the Comp.unix.questions
mailing list