64 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| #! /bin/bash
 | |
| #
 | |
| # original from:
 | |
| # newext: change filename extension
 | |
| # @(#) newext.sh 1.1 93/04/13
 | |
| # 90/06/06 john h. dubois iii (john@armory.com)
 | |
| # 90/11/14 changed ksh-specific code to hybrid: if running under Bourne,
 | |
| #          uses expr instead of ksh builtin ops.  Removed SYSV specific code.
 | |
| # 91/08/06 added -t option
 | |
| # 92/11/06 made earlier code actually work!
 | |
| # 93/04/13 If no filenames given, act on files in current dir
 | |
| #
 | |
| # conversion to bash v2 syntax by Chet Ramey
 | |
| 
 | |
| usage="Usage: newext [-th] <oldext> <newext> [filename ...]"
 | |
| 
 | |
| phelp()
 | |
| {
 | |
| echo "$usage
 | |
| Rename all given files that end in oldext with newext replacing oldext.
 | |
| If no filenames are given, all files in the current directory that end
 | |
| in oldext are acted on (no filename is equivalent to '*').
 | |
| Options:
 | |
| -h: Print this help.
 | |
| -t: Test: No action is taken except to print the mv commands that would
 | |
| be executed if -t was not given."
 | |
| }
 | |
| 
 | |
| while getopts "th" opt; do
 | |
|     case "$opt" in
 | |
|     t) echo=echo;;
 | |
|     h) phelp; exit 0;;
 | |
|     *) echo "$usage" 1>&2; exit 2;;
 | |
|    esac
 | |
| done
 | |
| 
 | |
| shift $((OPTIND - 1))
 | |
| 
 | |
| oldext=$1
 | |
| newext=$2
 | |
| 
 | |
| case $# in 
 | |
| [01])	echo -e "$usage\nUse -h for help." 1>&2; exit 2;;
 | |
| 2)	shift ; shift; set -- *;;
 | |
| *)	shift ; shift;;
 | |
| esac
 | |
| 
 | |
| found=
 | |
| 
 | |
| for file
 | |
| do
 | |
|     case "$file" in
 | |
|     *$oldext) 
 | |
| 	newname="${file%$oldext}$newext"
 | |
| 	$echo mv "$file" "$newname"
 | |
| 	found=true;;
 | |
|     esac
 | |
| done
 | |
| 
 | |
| if [ -z "$found" ]; then
 | |
|     echo "No files ending in \"$oldext\"."
 | |
|     exit 1
 | |
| fi
 | |
| exit 0
 | 
