How to do mv *.xyz *.abc in shell script??

Jim Rogers jimr at hp-lsd.COS.HP.COM
Tue Sep 11 03:09:46 AEST 1990


Following is a ksh script which I find useful for this purpose:


#!/bin/ksh

# This script changes the ending patterns in a set of files.
# The pattern to look for  is described as the argument to the "-f"
# option.  The pattern to create is described as the argument to the "-t"
# option.  The script will work on the list of file names following the
# options.

set -- `getopt f:t: $*`
if [ $? -ne 0 ]
then
	print -u2 "Usage: $0: -f current_pattern -t new_pattern file ..."
	exit 1
fi

old=""
new=""

for opt in $*
do
	case $opt in
		-f)	old="$2"; shift 2;;
		-t)	new="$2"; shift 2;;
		--)	shift; break ;;
	esac
done
if [ "$old" = "" ]
then
	print -u2 "Error $0: Must specify an ending pattern to change."
	exit 2
fi
for file in $*
do
	prefix=${file%$old}
	if [ "$prefix" != "$file" ]
	then
		newfile="$prefix""$new"
		mv "$file" "$newfile"
	fi
done



-------------------------------------------------------------------------------
Jim Rogers
Logic Systems Division
Hewlett Packard Company



More information about the Comp.unix.shell mailing list