blob: 5707eb089af66e449ef83cc916d194bb44312a3e (
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
|
#!/bin/sh
# Remove whitespace and certain characters from filenames in a directory
[ "$#" -gt 1 ] && {
printf "Usage: rmw [dir]\n" >&2
exit 1
}
dir=${1:-$(pwd)}
cd -- "$dir" || {
printf "Couldn't cd to directory.\n" >&2
exit 1
}
for file in .* *; do
[ -e "$file" ] || continue
newname=$(printf '%s\n' "$file" |
sed -E \
-e 's/["`”“-]*//g' \
-e 's/[[:space:]]+/_/g')
[ "$file" = "$newname" ] && continue
if [ -e "$newname" ]; then
printf "Skipping '%s' (target exists)\n" "$file" >&2
continue
fi
mv -- "$file" "$newname"
done
|