MVA: rename with wildcards

I think mva is a pretty good solution to the "flexible rename problem" on unix systems. You often want to rename a set of files which match a pattern, and it's a big pain to do it using "for" loops or perl. Here's how mva would rename a block of files ending in "htm" so that they end in "html". The first line is what you type, the later lines are output you might see.
  $ mva  *.htm  *.html 
  mv "file1.htm"   "file1.html"
  mv "gorilla.htm" "gorilla.html"
Here's how to name all files of the form stem.123.png to stem_123.png
 $ mva  *.*.png  *_*.png 
 mv "stem.000.png" "stem_000.png"
 mv "stem.001.png" "stem_001.png"
 mv "stem.002.png" "stem_002.png"
Here's how to convert all your PPM files to PNG
 $ mva  pnmtopng  *.ppm  ">"  *.png 
 pnmtopng "file1.ppm" > "file1.png"
 pnmtopng "stem.000.ppm" > "stem.png"
Why the double quotes on the command line? Well, without them the shell will try to make a file called "*.png". If you have one PNG file in your directory that would be disastrous.


OK, so I lied already. The above commands don't actually perform the renames, they just print a sequence of "mv" commands to standard output. You should always check these commands to be sure that it's going to do what you want it to do. When you're sure they look OK, add the magic -e switch to the command:

  mva -e *.htm *.html

[For hackers: Actually I prefer to just pipe the output through the shell. For example, "mva *.htm *.html |sh", and then you can get verbose output using "sh -v"].

FAQ

Why don't you just use X?

Where X is some other tool that does a similar thing. Here are some answers:
rename
Certain linux distributions come with a progam called rename, which offers a subset of mva's functionality. Its problem is that it renames in a weird way which can lead to potential problems. If you execute
rename png jpg *.png
in a directory which contains the file png001.png, you will see the following rename:
  png001.png  jpg001.png
which is probably not what you wanted. On the other hand, "mva *png *jpg" will do the right thing. Of course in this case, "rename .png .jpg *.png" will work correctly, but not if you have periods somewhere else in the filename.
chsuf
chsuf is more limited and more arcane. It doesn't allow anything much more complex than adding or deleting a suffix, and it's difficult to remember how to use it, because of different --add --delete options. For example, here's the standard HTM-to-HTML example in chsuf:
  chsuf --from="htm" --to="html" *.htm
shtool move
shtool move is much closer to mva, and indeed allows a more general replacement syntax -- the order of the replacements can be specified using "%1" "%2" etc. If shtool had existed when I wrote mva, I would probably not have bothered.

I still say however, that for most work, it's more convenient to write

  mva *.htm *.html
Than
  shtool move -e *.htm %1.html
but I agree that's pretty arguable.

I used mva and now "ls *" prints a "*"!

If you interrrupt mva while it's working, you can end up with your shell in "noglob" mode. Just type "mva" on its own and all will be restored.

Installation

The three files needed are in mva-1.1.zip and mva-1.1.tar.gz

First you need to copy the perl script "mva.pl" into a directory in your PATH.

Then if you use BASH, copy mva.bashrc into your ~/.bashrc. Its contents are:

# beginning of .bashrc modifications for MVA
# Add this to your .bashrc to use mva
alias mva='set -o noglob ; mva_exec_and_reset_glob perl -S mva.pl '
mva_exec_and_reset_glob () { 
    $* 
    set +o noglob
}
# end of .bashrc modifications for MVA

If you use TCSH, copy mva.tcshrc into ~/.tchsrc. Its contents are

# beginning of .tcshrc modifications for MVA
alias mva '(set noglob; exec perl -S mva.pl \!*)'
# end of .tcshrc modifications for MVA

If you use another shell, you'll have to use MVA by calling the perl script directly and quoting the wildcards. For example, if you copy mva.pl to mva and make it executable, then you would say

 mva "*.htm" "*.html"
If you use another shell and can figure out the incantation equivalent to the two above to turn off globbing for a single command, please let me know.
awf@robots.ox.ac.uk
Last modified: Wed Aug 27 14:11:51 GMT Daylight Time 2003