ren



Renames files with names that are similar




 Received this script from Don Peterson who got it from a net post.
 Purpose: Renames a bunch of files.
 Searches for a specified pattern in the name and changes to the 
 specified new pattern.
 i.e. If you had a 3 file called testfile1, testfile2, & testfile3.
 You could change them to pestfile1, pestfile2, and pestfile3.
 The pattern (which can be any valid sed s/pattern/) which should be
 changed, is specified before the files e.g.
 To change "test" to "pest" in every filename where test exists:
            ren test pest *
 Filenames which do not contain the pattern are left unchanged.

#!/bin/sh

# Below:
# Basename returns just the name of the script with the rest of the path
# stripped off and puts the value into the variable USAGE.
# Next, the script name stored in $USAGE  gets put back into itself (so to speak)
# along with the string that follows.

USAGE=`basename $0`                
USAGE=$USAGE'  pattern  substitute  files...'   


if test $# -lt 3               # If there aren't at least 3 parameters on the
then                           # command line  echo the string in $USAGE and
  echo "  USAGE:  $USAGE"      # exit.
  exit 1
fi

PATTERN=$1           # PATTERN equals the first argument on the command line.
shift                # Shift to the next argument and make it $1.
SUBSTITUTION=$1      # SUBSTITUTION equals the second parameter.
shift                # Shift to the next argument and make it $1.

  # Below:
  # If no list is specified in the for loop, it evaluates all the 
  # arguments on the command. 
  # echos each file name on the command line and pipes it to sed.
  # sed searches the name for the $PATTERN  and replaces it with $SUBSTITUTION
  # putting the new name in $x.
  # If the original file name ($i) is not equal to the new file name ($x)
  # then move $i to $x.

for i
 do  
  x=`echo $i | sed "s/$PATTERN/$SUBSTITUTION/"`
  if test $i != $x
  then 
    mv $i $x
  fi
done