Skip to content

Instantly share code, notes, and snippets.

@weehong-1
Last active March 6, 2026 07:44
Show Gist options
  • Select an option

  • Save weehong-1/a6205f99be440bf45a3572e4cd52184e to your computer and use it in GitHub Desktop.

Select an option

Save weehong-1/a6205f99be440bf45a3572e4cd52184e to your computer and use it in GitHub Desktop.
Rename Project name thoroughly for Java

How to use

curl -fsSL https://gist.githubusercontent.com/weehong-1/a6205f99be440bf45a3572e4cd52184e/raw/rename.sh | bash -s -- horaion upmatches
#!/bin/bash
# Check if the correct number of arguments are passed
if [ "$#" -ne 2 ]; then
echo "❌ Error: Missing arguments."
echo "πŸ‘‰ Usage: $0 <old_project_name> <new_project_name>"
exit 1
fi
# 1. Lowercase
OLD_LOWER=$(echo "$1" | tr '[:upper:]' '[:lower:]')
NEW_LOWER=$(echo "$2" | tr '[:upper:]' '[:lower:]')
# 2. Capitalized
OLD_CAP="$(echo "$OLD_LOWER" | cut -c1 | tr '[:lower:]' '[:upper:]')$(echo "$OLD_LOWER" | cut -c2-)"
NEW_CAP="$(echo "$NEW_LOWER" | cut -c1 | tr '[:lower:]' '[:upper:]')$(echo "$NEW_LOWER" | cut -c2-)"
# 3. UPPERCASE
OLD_UPPER=$(echo "$OLD_LOWER" | tr '[:lower:]' '[:upper:]')
NEW_UPPER=$(echo "$NEW_LOWER" | tr '[:lower:]' '[:upper:]')
echo "πŸš€ Preparing to rename project..."
echo " Lowercase: $OLD_LOWER -> $NEW_LOWER"
echo " Capitalized: $OLD_CAP -> $NEW_CAP"
echo " UPPERCASE: $OLD_UPPER -> $NEW_UPPER"
echo "----------------------------------------"
echo "🧹 Step 1: Cleaning up build directories..."
rm -rf .git .idea .settings .project .classpath target build
echo "πŸ“ Step 2: Replacing text inside files..."
# Exact matches first, then a case-insensitive fallback to catch weird mixtures
find . -type f \
-not -name "*.jar" \
-not -name "*.ico" \
-not -name "*.png" \
-not -name "$(basename "$0")" \
-exec perl -pi -e "s/$OLD_UPPER/$NEW_UPPER/g; s/$OLD_CAP/$NEW_CAP/g; s/$OLD_LOWER/$NEW_LOWER/g; s/$OLD_LOWER/$NEW_LOWER/gi" {} +
echo "πŸ“ Step 3: Renaming directories and files..."
# Rename UPPERCASE instances
find . -depth -name "*$OLD_UPPER*" | while read -r path ; do
dir=$(dirname "$path")
base=$(basename "$path")
newbase=$(echo "$base" | sed "s/$OLD_UPPER/$NEW_UPPER/g")
mv "$path" "$dir/$newbase"
done
# Rename Capitalized instances
find . -depth -name "*$OLD_CAP*" | while read -r path ; do
dir=$(dirname "$path")
base=$(basename "$path")
newbase=$(echo "$base" | sed "s/$OLD_CAP/$NEW_CAP/g")
mv "$path" "$dir/$newbase"
done
# Rename lowercase and fallback instances (case-insensitive)
find . -depth -iname "*$OLD_LOWER*" | while read -r path ; do
dir=$(dirname "$path")
base=$(basename "$path")
# Convert whatever case it is to the new lowercase name to standardize it
newbase=$(echo "$base" | grep -i "$OLD_LOWER" | perl -pe "s/$OLD_LOWER/$NEW_LOWER/gi")
if [ ! -z "$newbase" ] && [ "$base" != "$newbase" ]; then
mv "$path" "$dir/$newbase"
fi
done
echo "βœ… Script finished running!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment