-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass-rename.bash
118 lines (95 loc) · 2.94 KB
/
mass-rename.bash
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
#!/bin/bash
shopt -s extglob
function func_help()
{
echo " --help or -h: this help"
echo " --basename=<name to pre-pend when making new file> or -b=<>"
echo " --suffix=<filename> where filename must match this suffix ex: MTS of name.MTS files -s=<>"
echo " --donothing or --testrun or -d Dont actuall do anything, just output commands"
echo " --move -mv -m Move do not copy files"
echo " --dir=<dir>rectory in which to perform action"
echo " --verbose|-v Verbose output"
}
CMD="cp"
start_dir=`pwd`
for arg in "$@" ; do
#echo "DELME0: Argument: ${arg}"
case $arg in
--help|-h)
func_help
exit
;;
--basename=*|-b=*)
if [[ "" != "$basename" ]] ; then
echo "Warning: Basename was already set to ${basename}. Resetting to new value."
fi
basename=`echo ${arg} | awk -F'=' '{print $2}'`
;;
--suffix=*|-s=*)
if [[ "" != "$suffix" ]] ; then
echo "Warning: Suffix was already set to ${suffix}. Resetting to new value."
fi
suffix=`echo ${arg} | awk -F'=' '{print $2}'`
;;
--dono*|--testrun|-d)
test_run="true"
;;
--move*|--mv|-m)
CMD="mv"
;;
--dir=*)
newdir=`echo ${arg} | awk -F'=' '{print $2}'`
;;
--verbose|-v)
verb="true"
;;
*)
echo "Not sure what to do with: $arg .. Ignoring..."
func_help
exit
;;
esac
done
if [[ "" != ${verb} ]] ; then echo "newdir=${newdir}" ; fi
cd "$newdir"
cwd=`pwd`
if [[ "" == "${basename}" ]] ; then
basename=`basename "${cwd}"`
fi
if [[ "" != ${verb} ]] ; then echo "basename=${basename}" ; fi
if [[ "" != ${verb} ]] ; then echo "suffix=${suffix}" ; fi
if [[ "" != ${verb} ]] ; then echo "CMD=${CMD}" ; fi
if [[ "" != ${verb} ]] ; then echo "test_run=${test_run}" ; fi
if [[ "" != ${verb} ]] ; then echo "start_dir=${start_dir}" ; fi
if [[ "" != ${verb} ]] ; then echo "working dir=${cwd}" ; fi
file_list=*
#for file in "${file_list}" ; do
for file in * ; do
file_suf=`echo ${file} | awk -F'.' '{print $NF}'`
file_bad_basename=`echo ${file} | awk -F'.' '{$NF="";print $0}'`
file_basename=`echo ${file_bad_basename} | sed 's/ $//'`
base_match=""
#HACK ALERT: every file has a space placed at the end making the tests fail
if [[ ! -f $file ]] ; then
if [[ "" != ${verb} ]] ; then echo -e "\e[33mskipping\e[0m \"${file}\" (not a file after all)" ; fi
elif [[ `echo -n ${file_basename} | sed 's/ $//' | grep -E '^[0-9]+$'` ]] ; then
base_match="true"
elif [[ `echo -n ${file_basename} | grep -E '^CIMG[0-9]+ $'` ]] ; then
base_match="true"
else
if [[ "" != ${verb} ]] ; then echo -e "\e[33mskipping\e[0m \"${file}\" (no base match: ${file_basename})" ; fi
fi
if [[ "" != "${base_match}" ]] ; then
if [[ "${file_suf}" == "${suffix}" ]] ; then
newname=${basename}_${file}
if [[ "" != "$test_run" ]] ; then
echo -e "\e[36mtestrun:\e[0m ${CMD} ${file} ${newname}"
else
echo -e "${CMD} ${file} ${newname}"
$CMD "${file}" "${newname}"
fi
else
echo -e "\e[33mskipping\e[0m \"${file}\" (no suffix match)"
fi
fi
done