Help with script env var for mkdir for season dir if does not exist

Sonarr version (exact version):
Mono version (if Sonarr is not running on Windows):
OS: Ubuntu 20.04 LTS
Debug logs: N/A (for a new script)
Description of issue: Script environment variable for mkdir for new seasons within existing series

Additional Information:
Can someone with far better scripting skills help me? I currently use a simple bash script to copy episodes to a mirror location through Connect.

cp -p “$sonarr_episodefile_path” “{$sonarr_episodefile_path/tvshows/tvshowsbu}”

This takes the original path of:
/media/tvshows/TV Shows/Series/Season/Episode

and substitutes tvshowsbu for tvshows as intended and copies it to:
/media/tvshowsbu/TV Shows/Series/Season/Episode

It works great until a new season requires a new directory to be be created. Previously I manually created the new seasons, but this has gotten to be very tedious and I want to create the season directory on demand. I have been trying to figure out the correct “environment variable for season” and modify to something like:

File=“environment variable for season”
if [ ! -d “FILE” ]
mkdir -p “FILE” && cp -p “$sonarr_episodefile_path” “$sonarr_episodefile_path/tvshows/tvshowsbu}”
cp -p “$sonarr_episodefile_path” “{$sonarr_episodefile_path/tvshows/tvshowsbu}”

Even once the environment variable for the season is figured out, I am pretty sure this is not the cleanest bash script in the world. Any advise from those far more knowledgeable in scripting would be greatly appreciated.

There’s no need for a script for this. Why would you need to create season folders during an episode import?

Every series has an option to turn on or turn off Season folders.

All variables are noted in the docs

Unless I am totally missing something, it does not and has never created anything for the backup copies, which is what the script is for. This was something Markus originally helped with years ago on using a substitution with the environment variable sonarr_episode_path in a script to get it to copy on import/upgrade from the /media/tvshows to /media/tvshowsbu. The issue has always been that cp nor rsync can create a directory that does exist; ie new seasons, as part of the copy. If the season folder is manually created, the cp works fine. I am trying to create the season folder on the backup location so cp will work.

Maybe take out the guesswork and share the script you have now?

Why does the “additional information” area of the original post disappear? It had the scripts in it, but even I cannot see the rest of my original post now. Regardless, that is a different issue. My drive paths are /media/tvshows/ and the backup is /media/tvshowsbu/. The current script for copying is a simple cp -p using the existing provided environment variable sonarr_episodefile_path and substituting tvshowsbu for tvshows to copy each new episode to the backup drive:

cp -p “$sonarr_episodefile_path” “${sonarr_episodefile_path/tvshows/tvshowsbu}”

I was looking at inserting an if…then for a mkdir to create the season folder if not exists, but I cannot figure out a workable variable for the full path through to the season folder. There does not seem to be an existing environment variable for this although it would be hugely beneficial. The new script would look something like:

SEADIR="/mount/path/series/season no"
if [! -d “$SEADIR” ] then
mkdir -p “$SEADIR”
cp -p “$sonarr_episodefile_path” “${sonarr_episodefile_path/tvshows/tvshowsbu}”

Hope this clarifies. By figuring out a workable SEADIR, it is really easy to use Connect to copy on Import/Upgrade each new episode and maintain the backup copy of all series realtime.

Because you posted it in a comment block that doesn’t get posted publicly.

You’d want to get the path of the folder the file is in, then mkdir that path in the backup directory (with whatever flag will create the subfolder and parent folders if the series folder doesn’t exist).

Thanks for letting me know about the comment block not being public. I was assuming everyone could see and helped out with community responses.

Also, thanks for the link. It never showed up in any of my searches, but had what I needed. I am including the working script in case others have a similar need. It copies the latest download to the backup drive, and creates the season folders if not existing.

DIR="$(dirname “${VAR}”)" ; FILE="$(basename “${VAR}”)"
if [ ! -d “${DIR/tvshows/tvshowsbu}” ]; then
mkdir -p “${DIR/tvshows/tvshowsbu}”
cp -p “$sonarr_episodefile_path” “${sonarr_episodefile_path/tvshows/tvshowsbu}”