27 lines
656 B
Bash
27 lines
656 B
Bash
#!/usr/bin/env bash
|
|
|
|
# Usage:
|
|
# ./fix_test.sh Test # preview
|
|
# ./fix_test.sh Test apply # rename
|
|
|
|
pattern="${1:?Usage: $0 PATTERN [apply]}"
|
|
apply="${2:-}"
|
|
|
|
find . -type f -print0 |
|
|
while IFS= read -r -d '' f; do
|
|
base=$(basename "$f")
|
|
dir=$(dirname "$f")
|
|
|
|
newbase=$(printf '%s\n' "$base" |
|
|
sed -E "s/${pattern}([A-Z])/${pattern}_\1/g")
|
|
|
|
if [[ "$base" != "$newbase" ]]; then
|
|
if [[ "$apply" == "apply" ]]; then
|
|
mv -- "$f" "$dir/$newbase"
|
|
printf 'RENAMED: %s -> %s\n' "$f" "$dir/$newbase"
|
|
else
|
|
printf 'WOULD RENAME: %s -> %s\n' "$f" "$dir/$newbase"
|
|
fi
|
|
fi
|
|
done
|