-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_file_names.sh
More file actions
executable file
·182 lines (159 loc) · 4.53 KB
/
fix_file_names.sh
File metadata and controls
executable file
·182 lines (159 loc) · 4.53 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#
# Time-stamp: <Monday 2023-04-07 11:48:10 +1000 Jess Moore>
#
# Fix a specific file name or all files in a directory
#
# - replaces " " with "_"
# - replaces " - " with "-"
function usage() {
echo "Usage: $0 [-d] file [year]"
echo ""
echo "Description: Converts Windows style filenames with spaces to "
echo "linux style whitespace free filenames, either for all files in "
echo "the current directory or a specified file. Only one of '-d' or "
echo "'-y' may be used."
echo ""
echo "Arguments:"
echo " file: Name of file to fix the filename of. (No default)."
echo " year: Year of file creation/publication. (Default: current year)."
echo ""
echo "Flags:"
echo " -d: Prepend file with last modified date. (Default: false)."
echo " -y: Prepend file with user provided year or current year. (Default: false)."
echo ""
exit 1 # Exit with a non-zero status to indicate an error
}
if [[ $* == *"help"* || $* == *"-h"* ]]; then
usage
fi
# By default, is_shell is false
show_mod_date=false
show_year=false
# Parse arguments and flags
while getopts :hdy opt; do
case $opt in
h) usage;;
d) show_mod_date=true;;
y) show_year=true;;
:) echo "Missing argument for option -$OPTARG"; exit 1;;
\?) echo "Unknown option -$OPTARG"; exit 1;;
esac
done
# Shift positional parameters to remove processed options
shift $(( OPTIND - 1 ))
# Exit if user selected show_year and show_mod_date
if [[ $show_year == true && $show_mod_date == true ]]; then
usage
fi
if [[ -n "$2" ]]; then
YEAR="$2"
elif [[ $show_year ]]; then
YEAR=$(date "+%Y")
fi
# Filename supplied
if [[ -n "$1" && -e "$1" ]]; then
f="$1"
if [[ -n $f ]]; then
# Replace " " with "_"
g="${f// /_}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced ' ' with '_'"
f=$g # Update f
else
echo "No spaces found in filename."
fi
# Replace " - " with "-"
g="${f//_-_/-}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced ' - ' with '-'"
f=$g # Update f
else
echo "No hyphens in spaces found in filename."
fi
# Replace "," with ""
g="${f//,/}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced ',' with ''"
f=$g # Update f
else
echo "No commas found in filename."
fi
# Replace "+" with "_"
g="${f//+/_}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced '+' with '_'"
f=$g # Update f
else
echo "No + found in filename."
fi
# Replace double underscore "__" with "_"
g="${f/__/+/_}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced '__' with '_'"
f=$g # Update f
else
echo "No __ found in filename."
fi
# Replace "[]" with ""
g="${f//[][]/}"
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced '[]' with ''"
f=$g # Update f
else
echo "No [] found in filename."
fi
# Replace "()" with ""
# sed better than parameter expansion
# shellcheck disable=SC2001
g=$(echo "$f" | sed 's/(\([^)]*\))/\1/g')
if [ "$f" != "$g" ]; then
mv "$f" "$g"
echo "Replaced '()' with ''"
f=$g # Update f
else
echo "No () found in filename."
fi
# Prepend with last modified date
if $show_mod_date; then
DATE_SUMM=$(stat -f %Sm -t %Y%m%d "$f")
g="${DATE_SUMM}-$f"
mv "$f" "$g"
echo "Prepending file with today's date."
f=$g
fi
# Prepend with year
if $show_year; then
g="${YEAR}-$f"
mv "$f" "$g"
echo "Prepending file with year."
f=$g
fi
# Print recently changed matching files
# shellcheck disable=SC2012
ls -l "$f"
fi
else
# Run on all files
# Replace " " with "_"
for f in *\ *; do
if [[ -n $f ]]; then
mv "$f" "${f// /_}"
fi
done
# Replace " - " with "-"
for g in *_-_*; do
if [[ -n $g ]]; then
i=$(echo "$g" | tr '_-_' '-' | tr -s '-')
mv "$g" "$i"
fi
done
# Print filenames with most recent first
ls -lt
fi