Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created March 31, 2025 20:45
Show Gist options
  • Save mikeschinkel/912d95cf60ddaa16f83986896a1de15e to your computer and use it in GitHub Desktop.
Save mikeschinkel/912d95cf60ddaa16f83986896a1de15e to your computer and use it in GitHub Desktop.
Bash script to rename the directory a JetBrain's IDE project is contained in (I got tired of waiting for JetBrains to add this obviously needed feature to their actual IDE!)
#!/usr/bin/env bash
set -euo pipefail
# Check if both arguments are provided
if [ $# -ne 2 ]; then
echo "Rename a Jetbrains IDE project"
echo "Usage:"
echo " $(basename "$0") <old_project_name> <new_project_name> [<projects_dir default=~/Projects>]"
exit 1
fi
function echo_line {
echo "====================================================="
}
function main {
# Store arguments
local old_project="$1"
local new_project="$2"
local projects_dir="${3:-}"
local updated_files=0
if [ "${projects_dir}" == "" ]; then
projects_dir="${HOME}/Projects"
fi
# Check if directory exists
if [ ! -d "${projects_dir}" ]; then
echo "Error: Directory '${projects_dir}' does not exist"
exit 1
fi
# Find all text files and process them
while read -r file; do
# Check if file contains the search string
if ! grep -q "${old_project}" "${file}"; then
continue
fi
echo_line
echo "Updating ${file}:"
grep "${old_project}" "${file}"
# Replace the string and save changes
sed -i .bak "s/${old_project}/${new_project}/g" "${file}"
echo "To:"
grep "${new_project}" "${file}"
updated_files=$((updated_files + 1))
done < <(find "${projects_dir}/${old_project}/.idea" -type f -name "*.xml")
echo_line
local old_dir
local new_dir
old_dir="${projects_dir}/${old_project}"
new_dir="${projects_dir}/${new_project}"
echo "Renaming directory from:"
echo " ${old_dir}"
echo "To:"
echo " ${new_dir}"
mv "${old_dir}" "${new_dir}"
echo_line
local new_idea_dir
local old_iml
local new_iml
new_idea_dir="${new_dir}/.idea"
old_iml="${new_idea_dir}/${old_project}.iml"
new_iml="${new_idea_dir}/${new_project}.iml"
echo "Renaming file from:"
echo " ${old_iml}"
echo "To:"
echo " ${new_iml}"
mv "${old_iml}" "${new_iml}"
# Print summary
echo_line
echo "JeBrains IDE project '${old_project}' renamed to '${new_project}'"
echo " Old dir: ${old_dir}"
echo " New dir: ${new_dir}"
echo " ${updated_files} files updated."
echo " 1 directory renamed."
echo " 1 file renamed."
echo "Complete!"
exit 0
}
main "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment