#!/usr/bin/env bash # Navigate to the directory containing your files (optional, or run the script in that directory) cd '/path/to/folder' # Loop through all files in the current directory for old_name in *; do # Skip directories [ -f "$old_name" ] || continue # Use a regular expression to capture a two-digit number (\\d{2}) # This pattern assumes the number is surrounded by non-digit characters if [[ "$old_name" =~ (.*S02E)([0-9]{2})(.*(\.mkv|\.srt)$) ]]; then prefix="${BASH_REMATCH[1]}" number_str="${BASH_REMATCH[2]}" suffix="${BASH_REMATCH[3]}" # Convert the captured string to a number and perform arithmetic new_number=$((10#$number_str - 24)) # 10# forces base 10 interpretation # Format the new number to be two digits with leading zero if necessary new_number_str=$(printf "%02d" $new_number) # Construct the new filename new_name="${prefix}${new_number_str}${suffix}" # Check if the new name is different and then rename if [[ "$old_name" != "$new_name" ]]; then echo "Renaming: '$old_name' -> '$new_name'" # Uncomment the next line to actually perform the rename operation mv -v -- "$old_name" "$new_name" fi else echo "Skipping: '$old_name' (pattern not matched)" fi done