Skip to content

Instantly share code, notes, and snippets.

@joshcawthorne
Last active October 5, 2023 13:18
Show Gist options
  • Save joshcawthorne/25eafe4bd3ec75c7fb37e34aeb59d7ae to your computer and use it in GitHub Desktop.
Save joshcawthorne/25eafe4bd3ec75c7fb37e34aeb59d7ae to your computer and use it in GitHub Desktop.
Update files and folders from camelCase to PascalCase. This script is designed to recursively update the capitalization of both files and directories in the current directory. Intended to update a project from using camelCase to PascalCase.
#!/bin/bash
# --------------------------------------------------------------------------
# Script Author: @joshcawthorne
# Warning: BACKUP ALL YOUR FILES BEFORE RUNNING THIS - THIS IS A DESTRUCTIVE
# OPERATION, I AM NOT RESPONSIBLE FOR ANY LOST FILES.
# OS Compatibility: MacOS, Linux, and WSL on Windows 10/11.
# --------------------------------------------------------------------------
# Description:
# -
# This script is designed to recursively update the capitalization of both
# files and directories in the current directory. Intended to update a project
# from using camelCase to PascalCase.
#
# - For files: It targets extensions ".js", ".jsx", ".ts", and ".tsx",
# but excludes any "index.*" files.
# - For directories: It capitalizes the first letter of all directories.
# --------------------------------------------------------------------------
# Git Users:
# -
# Git is case-insensitive by default. Post-execution, explicit renaming in Git is needed.
# 1. Remove all files from Git's index: git rm -r --cached .
# 2. Re-add all files: git add .
# 3. Commit changes: git commit -m "Case-sensitive file renaming"
# --------------------------------------------------------------------------
# Instructions:
# -
# 1. Copy this file to the root of the directory you want to rename.
# 2. Run the following to make the file executable:
# chmod +x ./rename_files.sh
# 3. Now, run the script:
# ./rename_files.sh
# 4. Double, Triple, check that all your files and folders were updated
# correctly BEFORE committing to any source!
# 5. SUGGESTION: If you're using something such as React, you could use
# a codemod script to update all imports in your project:
# https://github.com/facebook/jscodeshift
# --------------------------------------------------------------------------
# `find ./ -type f` finds all files in the directory.
# `-iname "*.js" -o -iname "*.jsx" -o -iname "*.ts" -o -iname "*.tsx"` filters only files with the specified extensions.
# `! -iname "index.*"` excludes any index files.
find ./ -type f \( -iname "*.js" -o -iname "*.jsx" -o -iname "*.ts" -o -iname "*.tsx" \) ! -iname "index.*" | while read -r filepath; do
# Using awk to capitalize the first letter of the filename
# The awk logic is built to respect existing capitalization.
newname=$(basename "$filepath" | awk 'BEGIN{FS=OFS=""} { for(i=1;i<=length($0);i++) { if(i==1 || substr($0,i-1,1)=="-") { $i=tolower($i) } } } 1')
# Check if the new filename is different from the old one.
if [ "$newname" != "$(basename "$filepath")" ]; then
# Perform the rename operation.
mv "$filepath" "$(dirname "$filepath")/$newname"
fi
done
# `find ./ -type d` finds all directories.
find ./ -type d | while read -r dirpath; do
# Similar awk logic as above, but this time for directories.
newname=$(basename "$dirpath" | awk 'BEGIN{FS=OFS=""} { for(i=1;i<=length($0);i++) { if(i==1 || substr($0,i-1,1)=="-") { $i=tolower($i) } } } 1')
# Check if the new directory name is different from the old one.
if [ "$newname" != "$(basename "$dirpath")" ]; then
# Perform the rename operation.
mv "$dirpath" "$(dirname "$dirpath")/$newname"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment