Created
May 16, 2024 06:30
-
-
Save emigrating/9a927f46b393e4651a6654312ca7dac9 to your computer and use it in GitHub Desktop.
Script to rename certain files from a #### - Title.ext format to the more common S##E## - Title.ext scheme.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script to rename certain files from a #### - Title.ext format to | |
| # the more common S##E## - Title.ext scheme. | |
| mv_cmd=mv | |
| function display_help { | |
| echo "Usage: $0 [options]" | |
| echo " -i, --infile Input file" | |
| echo " -o, --outseason Output season" | |
| echo " -s, --startepisode Start episode" | |
| echo " -h, --help Display this help message" | |
| exit 1 | |
| } | |
| while [[ "$#" -gt 0 ]]; do | |
| case $1 in | |
| -i | --infile) infile="$2"; shift ;; | |
| -s | --outseason) outseason="$2"; shift ;; | |
| -e | --startepisode) startepisode="$2"; shift ;; | |
| -d | --dryrun) dryrun=true; action=echo; shift ;; | |
| -h | --help) display_help ;; | |
| *) echo "Unknown parameter passed: $1"; exit 1 ;; | |
| esac | |
| shift | |
| done | |
| # Extract the prefix from the infile | |
| prefix=$(echo $infile | grep -o -E '^.{2}') | |
| # Get the directory of the infile | |
| dir=$(dirname "$infile") | |
| # Initialize the episode number | |
| episode=$startepisode | |
| # Loop over all files in the directory that match the prefix | |
| for file in "$dir"/"$prefix"*; do | |
| # Extract the rest of the filename from the current file, removing the original episode number | |
| rest=$(echo $file | grep -o -E '^.{6}(.*)' | cut -c 7-) | |
| # Format the new filename | |
| new=$(printf "S%sE%02d%s" "$outseason" "$episode" "$rest") | |
| # Rename the file | |
| $action $mv_cmd -- "$file" "$new" | |
| echo "Renamed $file to $new" | |
| # Increment the episode number | |
| episode=$((episode + 1)) | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment