summaryrefslogtreecommitdiff
path: root/examples/scripts.v2/rename
blob: 96c46d685f5f4a54484f868538b3e7be977adce9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /bin/bash
#
# original from:
# @(#) rename.ksh 1.1 94/05/10
# 90/06/01 John DuBois (spcecdt@armory.com)
# 91/02/25 Improved help info
# 92/06/07 remove quotes from around shell pattern as required by new ksh
# 94/05/10 Exit if no globbing chars given.
#
# conversion to bash v2 syntax by Chet Ramey

phelp()
{
echo "$usage
All files that match oldpattern will be renamed with the
filename components that match the constant parts of oldpattern
changed to the corresponding constant parts of newpattern.
The components of the filename that match variable parts of
oldpattern will be preserved.  Variable parts in oldpattern
must occur in the same order in newpattern.  Variables parts
can be '?' and '*'.
Example: 
rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\"
All files in /tmp that match foo*.ba.? will have the \"foo\" part
replaced by \"new\" and the \".ba.\" part replaced by \"x\"."
}

usage="usage: $name [-htv] oldpattern newpattern"
name=${0##/}

while getopts "htv" opt; do
    case "$opt" in
    t) tell=true;;
    v) verbose=true;;
    h) phelp; exit 0;;
    *) echo "$name: $usage" 1>&2; exit 2;;
    esac
done
shift $((OPTIND - 1))

if [ $# -lt 2 ]; then
    phelp
    exit 2
fi

oldpat=$1
newpat=$2

set -- $1
if [ ! -e "$1" ]; then
    echo "$name: no files match $oldpat."
    exit 1
fi

typeset -i i=1 j

# Example oldpat: foo*.a
# Example newpat: bar*.b

# Examples given for first iteration (in the example, the only interation)
while :; do
    case "$oldpat" in
    *[\*\?]*)	;;
    *)		break;;
    esac

    # Get leftmost globbing pattern in oldpat
    pat=${oldpat#*[\*\?]}	# pat=.a
    pat=${oldpat%%"$pat"}	# pat=foo*
    pat=${pat##*[!\?\*]}	# pat=*
    # Find parts before & after pattern
    oldpre[i]=${oldpat%%"$pat"*}	# oldpre[1]=foo
    oldsuf[i]=${oldpat#*"$pat"}		# oldsuf[1]=.a
    newpre[i]=${newpat%%"$pat"*}	# newpre[1]=bar
    # Get rid of processed part of patterns
    oldpat=${oldpat#${oldpre[i]}"$pat"}	# oldpat=.a
    newpat=${newpat#${newpre[i]}"$pat"}	# newpat=.b
    let i=i+1
done

if [ $i -eq 1 ]; then
    echo "No globbing chars in pattern." 1>&2
    exit 1
fi

oldpre[i]=${oldpat%%"$pat"*}	# oldpre[2]=.a
oldsuf[i]=${oldpat#*"$pat"}	# oldsuf[2]=.a
newpre[i]=${newpat%%"$pat"*}	# newpre[2]=.b

if [ -n "$verbose" ]; then
    j=1
    while let "j < i"; do
	echo \
"Old prefix: ${oldpre[j]}   Old suffix: ${oldsuf[j]}   New prefix: ${newpre[j]}"
	let j=j+1
    done
fi

# Example file: foox.a

for file; do
    j=1
    origname=$file	# origname=foox.a
    newfile=
    while let "j <= i"; do
	# Peel off a prefix	interation	1		2
	file=${file#${oldpre[j]}}		# file=x.a	file=
	# Save the part of this prefix that is to be retained
	const=${file%${oldsuf[j]}}		# const=x	const=
	newfile=$newfile${newpre[j]}$const	# newfile=barx	newfile=barx.b
	file=${file#$const}			# file=.a	file=.a
	let j=j+1
    done
    if [ -n "$tell" ]; then
	echo "Would move \"$origname\" to \"$newfile\"."
    else
	if [ -n "$verbose" ]; then
	    echo "Moving \"$origname\" to \"$newfile\"."
	fi
	mv $origname $newfile
    fi
done