1#! /bin/bash 2# 3# original from: 4# newext: change filename extension 5# @(#) newext.sh 1.1 93/04/13 6# 90/06/06 john h. dubois iii (john@armory.com) 7# 90/11/14 changed ksh-specific code to hybrid: if running under Bourne, 8# uses expr instead of ksh builtin ops. Removed SYSV specific code. 9# 91/08/06 added -t option 10# 92/11/06 made earlier code actually work! 11# 93/04/13 If no filenames given, act on files in current dir 12# 13# conversion to bash v2 syntax by Chet Ramey 14 15usage="Usage: newext [-th] <oldext> <newext> [filename ...]" 16 17phelp() 18{ 19echo "$usage 20Rename all given files that end in oldext with newext replacing oldext. 21If no filenames are given, all files in the current directory that end 22in oldext are acted on (no filename is equivalent to '*'). 23Options: 24-h: Print this help. 25-t: Test: No action is taken except to print the mv commands that would 26be executed if -t was not given." 27} 28 29while getopts "th" opt; do 30 case "$opt" in 31 t) echo=echo;; 32 h) phelp; exit 0;; 33 *) echo "$usage" 1>&2; exit 2;; 34 esac 35done 36 37shift $((OPTIND - 1)) 38 39oldext=$1 40newext=$2 41 42case $# in 43[01]) echo -e "$usage\nUse -h for help." 1>&2; exit 2;; 442) shift ; shift; set -- *;; 45*) shift ; shift;; 46esac 47 48found= 49 50for file 51do 52 case "$file" in 53 *$oldext) 54 newname="${file%$oldext}$newext" 55 $echo mv "$file" "$newname" 56 found=true;; 57 esac 58done 59 60if [ -z "$found" ]; then 61 echo "No files ending in \"$oldext\"." 62 exit 1 63fi 64exit 0 65