45 lines
No EOL
1.3 KiB
Bash
Executable file
45 lines
No EOL
1.3 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Check if the required arguments are provided
|
|
if [ "$#" -ne 3 ]; then
|
|
echo "Usage: $0 <directory> <search_expression> <replace_expression>"
|
|
exit 1
|
|
fi
|
|
|
|
# Assign arguments to variables
|
|
directory="$1"
|
|
search_expr="$2"
|
|
replace_expr="$3"
|
|
|
|
# Check if the directory exists
|
|
if [ ! -d "$directory" ]; then
|
|
echo "Error: Directory '$directory' does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
# Function to rename files
|
|
rename_files() {
|
|
local dir="$1"
|
|
|
|
# Loop through all files and directories in the current directory
|
|
for item in "$dir"/*; do
|
|
if [ -d "$item" ]; then
|
|
# If it's a directory, recurse into it
|
|
rename_files "$item"
|
|
elif [ -f "$item" ]; then
|
|
# If it's a file, rename it if it matches the search expression
|
|
local filename=$(basename "$item")
|
|
if [[ "$filename" == *"$search_expr"* ]]; then
|
|
local new_filename="${filename//$search_expr/$replace_expr}"
|
|
local new_path="${item%/*}/$new_filename"
|
|
mv "$item" "$new_path"
|
|
echo "Renamed: $item -> $new_path"
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Start the renaming process
|
|
echo "Starting renaming process in directory: $directory"
|
|
rename_files "$directory"
|
|
echo "Renaming process complete." |