-
Notifications
You must be signed in to change notification settings - Fork 0
/
massRename
executable file
·78 lines (67 loc) · 1.28 KB
/
massRename
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
#!/bin/bash
# By Isaac Good
# Reads filenames from the arguements (eg *)
# Prints them to a file in pairs
# A FROM line and a TO line
# Then it reads the lines back and does a mv FROM TO
tmp=$(mktemp)
quit () {
rm "$tmp"
exit $1
}
warn () {
echo "$@" >&2
}
die () {
echo "$@" >&2
quit 1
}
for arg; do
echo "FROM $arg"
echo "TO $arg"
echo
done > "$tmp"
EDITOR="${EDITOR:-/usr/bin/vim}"
$EDITOR "$tmp"
if [[ ! -f $tmp ]] ; then
die "Tmp file gone!"
fi
# FROM here
FROM=""
# TO here
TO=""
# Expecting a from or to?
EXPECT="FROM"
# Only parse FROM and TO lines
egrep -e '^FROM' -e '^TO' "$tmp" | while read i; do
# If we are expecting a FROM, check the line says FROM and the file exists
if [[ $EXPECT = "FROM" ]] ; then
if [[ $i = "FROM "* ]]; then
FROM="${i#FROM }"
if [[ -f $FROM ]] ; then
# We now expect a TO to match this FROM file
EXPECT="TO"
else
warn "File does not exist! [$i]"
fi
else
warn "Expected a FROM and got a TO! [$i]"
fi
# If expecting a TO...
else
if [[ $i = "TO "* ]]; then
TO="${i#TO }"
if [[ ! -f $TO ]] ; then
mv -v "$FROM" "$TO"
else
# Don't overwrite!
warn "TO file exists already! [$i]"
fi
else
warn "Expected a TO and got a FROM! [$i]"
fi
# Expect a FROM now
EXPECT="FROM"
fi
done
quit 0