#!/bin/bash SOURCE_DIR="/path/to/source_directory" TARGET_DIR="/path/to/target_directory" # Iterate through files in the source directory for source_file in "$SOURCE_DIR"/*; do # Extract the filename without the path filename=$(basename "$source_file") # Example: Extract a specific pattern (e.g., "episode_01") from the source filename # This example assumes the pattern is between "prefix_" and ".ext" # Adjust the regex according to your specific filename pattern pattern=$(echo "$filename" | grep -oP 'prefix_\K[^.]+') # If a pattern is found if [[ -n "$pattern" ]]; then # Construct the new filename for the target file # This example assumes target files have a "placeholder_" prefix # and we want to replace it with the extracted pattern. # Adjust this logic to match your target file naming convention. new_name="new_prefix_${pattern}.ext" # Replace .ext with the actual extension # Find the corresponding file in the target directory (adjust pattern if needed) # This example looks for a file that contains the extracted pattern # and has the same extension as the source file. target_file_to_rename=$(find "$TARGET_DIR" -maxdepth 1 -type f -name "*${pattern}*.ext") # Replace .ext if [[ -n "$target_file_to_rename" ]]; then echo "Renaming '$target_file_to_rename' to '$TARGET_DIR/$new_name'" mv "$target_file_to_rename" "$TARGET_DIR/$new_name" else echo "No matching file found in $TARGET_DIR for pattern: $pattern" fi fi done