Skip to content

Instantly share code, notes, and snippets.

@jorcelinojunior
Created February 11, 2024 15:59
Show Gist options
  • Save jorcelinojunior/bafe17f3f7a1372aa34e7bb03e34356a to your computer and use it in GitHub Desktop.
Save jorcelinojunior/bafe17f3f7a1372aa34e7bb03e34356a to your computer and use it in GitHub Desktop.
Script to recursively rename directories and files from 'foo' to 'bar', with a simulation mode available.
#!/bin/bash
SEARCH_DIR="./src"
SEARCH_FOR="foo"
REPLACE_WITH="bar"
SIMULATE=false
# Function to rename matching files and directories
rename_matches() {
local path="$1"
# Directories
find "$path" -depth ! -path "$path" -type d -name "*$SEARCH_FOR*" | while read -r dir ; do
local new_dir="${dir//$SEARCH_FOR/$REPLACE_WITH}"
echo "Renaming directory: $dir -> $new_dir"
if [ "$SIMULATE" = false ] ; then
mv "$dir" "$new_dir"
else
# Update path for simulation
path="$new_dir"
fi
done
# Files
find "$path" -type f -name "*$SEARCH_FOR*" | while read -r file ; do
local dir_path=$(dirname "$file")
local new_dir_path="${dir_path//$SEARCH_FOR/$REPLACE_WITH}"
local new_file="${file//$SEARCH_FOR/$REPLACE_WITH}"
echo "Renaming file: $file -> $new_file"
if [ "$SIMULATE" = false ] ; then
mv "$file" "$new_file"
fi
done
}
# Check if the script should simulate operations
if [ "$1" = "--simulate" ] || [ "$1" = "-s" ]; then
SIMULATE=true
echo "Simulation mode activated. No changes will be made to files."
fi
# Execute renaming (or simulation)
rename_matches "$SEARCH_DIR"
echo "Process completed."
@jorcelinojunior
Copy link
Author

Rename Script Explanation and Usage

This Bash script is designed to streamline the process of bulk renaming directories and files in a project. It targets items containing the term "exercise," replacing them with "sentence," while maintaining the integrity of subdirectory and file structures.

Key Features:

  • Recursive renaming: Capable of processing nested directories and files.
  • Simulation mode: Offers a preview of changes without actually implementing them, ensuring you can review potential adjustments beforehand.

Setup and Execution:

  1. Create the script file: Copy the script content into a new file, for instance, rename_script.sh.
  2. Make the script executable: Run chmod +x rename_script.sh to grant execution permissions.
  3. Configure search parameters: Tailor the SEARCH_DIR, SEARCH_FOR, and REPLACE_WITH variables within the script to suit your specific requirements.
  4. Execute the script: Launch the script via ./rename_script.sh. To enable simulation mode and prevent any real changes, add the --simulate or -s flag when running the script.
  5. Apply changes: By default, the script performs actual renaming. To engage simulation mode for a dry run, ensuring no alterations are made, use the aforementioned flags.

Example:

Executing the script without simulation flags (--simulate or -s) will directly apply the renaming actions. To preview these changes without committing them, ensure you activate simulation mode. This feature is invaluable for ensuring your project's naming conventions remain consistent and accurate.

Before initiating the script, creating a project backup is advisable to mitigate any risk of data loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment