1#! /bin/bash 2# 3# original from: 4# @(#) rename.ksh 1.1 94/05/10 5# 90/06/01 John DuBois (spcecdt@armory.com) 6# 91/02/25 Improved help info 7# 92/06/07 remove quotes from around shell pattern as required by new ksh 8# 94/05/10 Exit if no globbing chars given. 9# 10# conversion to bash v2 syntax by Chet Ramey 11 12phelp() 13{ 14echo "$usage 15All files that match oldpattern will be renamed with the 16filename components that match the constant parts of oldpattern 17changed to the corresponding constant parts of newpattern. 18The components of the filename that match variable parts of 19oldpattern will be preserved. Variable parts in oldpattern 20must occur in the same order in newpattern. Variables parts 21can be '?' and '*'. 22Example: 23rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\" 24All files in /tmp that match foo*.ba.? will have the \"foo\" part 25replaced by \"new\" and the \".ba.\" part replaced by \"x\"." 26} 27 28usage="usage: $name [-htv] oldpattern newpattern" 29name=${0##/} 30 31while getopts "htv" opt; do 32 case "$opt" in 33 t) tell=true;; 34 v) verbose=true;; 35 h) phelp; exit 0;; 36 *) echo "$name: $usage" 1>&2; exit 2;; 37 esac 38done 39shift $((OPTIND - 1)) 40 41if [ $# -lt 2 ]; then 42 phelp 43 exit 2 44fi 45 46oldpat=$1 47newpat=$2 48 49set -- $1 50if [ ! -e "$1" ]; then 51 echo "$name: no files match $oldpat." 52 exit 1 53fi 54 55typeset -i i=1 j 56 57# Example oldpat: foo*.a 58# Example newpat: bar*.b 59 60# Examples given for first iteration (in the example, the only interation) 61while :; do 62 case "$oldpat" in 63 *[\*\?]*) ;; 64 *) break;; 65 esac 66 67 # Get leftmost globbing pattern in oldpat 68 pat=${oldpat#*[\*\?]} # pat=.a 69 pat=${oldpat%%"$pat"} # pat=foo* 70 pat=${pat##*[!\?\*]} # pat=* 71 # Find parts before & after pattern 72 oldpre[i]=${oldpat%%"$pat"*} # oldpre[1]=foo 73 oldsuf[i]=${oldpat#*"$pat"} # oldsuf[1]=.a 74 newpre[i]=${newpat%%"$pat"*} # newpre[1]=bar 75 # Get rid of processed part of patterns 76 oldpat=${oldpat#${oldpre[i]}"$pat"} # oldpat=.a 77 newpat=${newpat#${newpre[i]}"$pat"} # newpat=.b 78 let i=i+1 79done 80 81if [ $i -eq 1 ]; then 82 echo "No globbing chars in pattern." 1>&2 83 exit 1 84fi 85 86oldpre[i]=${oldpat%%"$pat"*} # oldpre[2]=.a 87oldsuf[i]=${oldpat#*"$pat"} # oldsuf[2]=.a 88newpre[i]=${newpat%%"$pat"*} # newpre[2]=.b 89 90if [ -n "$verbose" ]; then 91 j=1 92 while let "j < i"; do 93 echo \ 94"Old prefix: ${oldpre[j]} Old suffix: ${oldsuf[j]} New prefix: ${newpre[j]}" 95 let j=j+1 96 done 97fi 98 99# Example file: foox.a 100 101for file; do 102 j=1 103 origname=$file # origname=foox.a 104 newfile= 105 while let "j <= i"; do 106 # Peel off a prefix interation 1 2 107 file=${file#${oldpre[j]}} # file=x.a file= 108 # Save the part of this prefix that is to be retained 109 const=${file%${oldsuf[j]}} # const=x const= 110 newfile=$newfile${newpre[j]}$const # newfile=barx newfile=barx.b 111 file=${file#$const} # file=.a file=.a 112 let j=j+1 113 done 114 if [ -n "$tell" ]; then 115 echo "Would move \"$origname\" to \"$newfile\"." 116 else 117 if [ -n "$verbose" ]; then 118 echo "Moving \"$origname\" to \"$newfile\"." 119 fi 120 mv $origname $newfile 121 fi 122done 123