blob: 37645bdad93fe1b78a53d089a401b8d3fff712f7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#! /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
|