My "multiple move" shell script

By Alvin J. Alexander, devdaily.com

Here's one of my favorites shell scripts. I use it to change the extensions of a bunch of files all at once. For instance, I just had a situation where I got a bunch of files with an extension of .PNG, using capital letters like that. That's no big deal if you have a few files to deal with, but what if you have 200 or more? That's where this script, named mmv, comes in.

Usage is simple, like this:

mmv PNG png

Output from the script looks like this:

Moving slide1.PNG --> slide1.png
Moving slide10.PNG --> slide10.png
Moving slide11.PNG --> slide11.png

As you can see, all the upper-case PNG extensions are changed to lower-case png strings.

The way it's coded right now it's a little weak. It looks for the first decimal in a filename, so if you have more than one this script will not work right. I'll change it one day if it ever becomes really important, but it hasn't been a problem for me yet.

Here then is the source code for the mmv (multiple move) shell script:

#!/bin/sh
#
# mmv (from devdaily.com):
# ------------------------
# move all the files with the first filename extension to the second extension.
#
# example 1:
#
#    mmv htm html  
#    (this moves all *.htm to *.html)
#
# example 2:
#
#    mmv PNG png  
#    (this moves all *.PNG to *.png (effectively changing the case of all files)
#
# caution:
#
#    this script will not work right on filenames that have more 
#    than one decimal in the name.


if [ -z "$1" ] || [ -z "$2" ]
then
  echo "Usage: mmv oldExtension newExtension"
  exit -1
fi

for i in `ls *${1}`
do
  orig=$i
  new=`echo $i | cut -f1 -d.`.${2}
  echo "Moving $orig --> $new"
  mv $orig $new
done

devdaily logo