#!/usr/bin/env bash # To clarify, this simply changes the file names in $TARGET_DIR to the names # of files in $SOURCE_DIR if they both match $grep_pattern. # # EXAMPLE: # SOURCE_DIR/good-name-S01E05-suffix.ext # TARGET_DIR/bad-name-S01E05-suffix.ext > TARGET_DIR/good-name-S01E05-suffix.ext SOURCE_DIR="/source/path" TARGET_DIR="/target/path" grep_pattern="S[0-9]{1,2}E[0-9]{1,2}" # 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 -Eo "$grep_pattern") # 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="$filename" # 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}*.mkv") # 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