Skip to content

Instantly share code, notes, and snippets.

@coltenkrauter
Last active September 11, 2023 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coltenkrauter/dfcef2507106c01f262b5758ffe8c9eb to your computer and use it in GitHub Desktop.
Save coltenkrauter/dfcef2507106c01f262b5758ffe8c9eb to your computer and use it in GitHub Desktop.
This guide illustrates how to meticulously organize movie files into individual directories within your Plex Media Library, either recursively or non-recursively, to streamline metadata management and enhance user experience.

Guide to Organizing Movie Files into Individual Folders

This comprehensive guide covers the process of moving movie files into individual folders for a neatly organized Plex Media Server library, and it also explains how to use FileBot to rename files and add posters and artwork for an enhanced media management experience.

Table of Contents

  1. Introduction
  2. Plex Media Server: Contextual Overview
  3. Scripts
  4. Setting Up a Simulated Movie Folder for Test Runs
  5. Renaming Files and Adding Posters with FileBot
  6. Further Reading
  7. Collaboration
  8. Credit

Introduction

Before we begin, it's always a good practice to backup your data to prevent any accidental loss. Also, we recommend performing a trial run on a subset of your data to ensure the script functions as expected. Now, let's dive in!

Plex Media Server: Contextual Overview

Plex Media Server is a popular choice for organizing and streaming media collections. A prevalent use case is setting up a movie library. However, you might find your collection cluttered with movies and additional metadata such as posters and trailers scattered across various folders. To make management and navigation easier, you might want to organize each movie along with its associated metadata into individual folders. This guide offers two zsh script options to assist you in achieving a neatly organized library.

Scripts

Option 1: Move Movie Files into Individual Folders (Non-Recursive)

This option allows you to move your movie files into individual folders without delving into subdirectories. Here's the script with brief comments explaining each step:

for f in *.(mp4|mkv|avi|mov)(N); do
  if [ -e "$f" ]; then
    mkdir "${f%.*}"
    mv "$f" "${f%.*}"
  fi
done

Example

Suppose you have a directory structure as follows:

/Movies
    movie1.mp4
    movie2.mkv
    movie3.avi
    movie4.mov

Running the script in the "/Movies" directory will move the movies into individual folders as follows:

/Movies
    /movie1
        movie1.mp4
    /movie2
        movie2.mkv
    /movie3
        movie3.avi
    /movie4
        movie4.mov

Option 2: Move Movie Files into Individual Folders (Recursive and Prevent Duplicate Folders)

This option is a more advanced solution that can delve into subdirectories and move files into individual folders. Moreover, this script is idempotent, meaning you can run it multiple times without changing the result beyond the initial application. It checks if the parent directory has the same name as the intended directory to prevent duplicate folders. Here is the script:

find . -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" -o -name "*.mov" -o -name "*.flv" -o -name "*.wmv" -o -name "*.m4v" -o -name "*.mpg" -o -name "*.mpeg" \) -exec sh -c '
    f="$1"
    d="$(dirname "$f")/$(basename "$f" .${f##*.})"
    parent_dir="$(dirname "$f")"
    base_parent_dir="$(basename "$parent_dir")"
    base_new_dir="$(basename "$d")"

    if [ ! -d "$d" ] && [ "$base_parent_dir" != "$base_new_dir" ]; then
        mkdir -p "$d"
        mv "$f" "$d/"
    fi
' sh {} \;

Example

Suppose you have a directory structure as follows:

/Movies
    /RomCom
        /movie2
            movie2.mp4
        movie1.mp4
    /Action
        movie3.mp4
        movie4.mp4

Running the script in the "/Movies" directory will result in:

/Movies
    /RomCom
        /movie1
            movie1.mp4
        /movie2
            movie2.mp4
    /Action
        /movie3
            movie3.mp4
        /movie4
            movie4.mp4

Setting Up a Simulated Movie Folder for Test Runs

Before running the script on your actual movie collection, it's prudent to test the script in a simulated environment to ensure it functions as expected. Here, we provide a zsh script to help you create a simulated movie folder structure that mimics the recursive option example. You can run this script to create a set of mock movie files and folders, and then run the organizing scripts on this simulated dataset to observe their effects firsthand.

# Change to the directory where you want to create the test data
cd /path/to/your/test/directory

# Create a nested directory structure with various movie files
mkdir -p Movies/RomCom/movie2
touch Movies/RomCom/movie2/movie2.mp4
touch Movies/RomCom/movie1.mp4
mkdir -p Movies/Action
touch Movies/Action/movie3.mp4
touch Movies/Action/movie4.mp4

After executing the above script, you will have a directory structure as follows:

/Movies
    /RomCom
        /movie2
            movie2.mp4
        movie1.mp4
    /Action
        movie3.mp4

Renaming Files and Adding Posters with FileBot

FileBot is a powerful tool that can help you organize your media library by renaming files and fetching additional metadata such as posters, artwork, and subtitles. Here, we'll explore how to use FileBot to enhance your Plex Media Server library organization.

Installing FileBot

Before you begin, you'll need to install FileBot on your system. You can download FileBot from the official website.

Once installed, open FileBot to get started.

Renaming Files

FileBot allows you to rename your media files according to your preferred naming convention. This is particularly useful for ensuring that Plex Media Server can recognize and organize your files correctly. To rename files:

  1. Launch FileBot.
  2. Drag and drop the media files you want to rename into the FileBot interface.
  3. Select the files in FileBot's file list.
  4. Click the "Rename" button.

FileBot will apply its built-in naming conventions, but you can customize these conventions to suit your preferences. You can also use data from online databases to name your files accurately.

Fetching Posters and Artwork

To make your media library visually appealing, you can use FileBot to fetch posters and artwork for your movies and TV shows. Here's how:

  1. Launch FileBot.
  2. Drag and drop the media files you want to fetch artwork for into the FileBot interface.
  3. Select the files in FileBot's file list.
  4. Click the "Match" button.

FileBot will query online databases such as TheTVDB and TheMovieDB to find matching titles and artwork for your media files. You can choose from the available options and apply the artwork to your files.

Saving Changes

Once you've renamed your files and added artwork, FileBot allows you to apply these changes to your media files. To save your changes:

  1. Select the files with the applied changes in FileBot's file list.
  2. Click the "Rename" button to save the renamed files.
  3. Click the "Move" button to move the renamed and organized files to your Plex Media Server library directory.

Conclusion

FileBot is an invaluable tool for organizing your media library, ensuring accurate file naming, and adding eye-catching posters and artwork to your collection. With FileBot's powerful features, you can enhance your Plex Media Server experience and enjoy a well-organized and visually appealing media library.

Use FileBot in conjunction with the scripts provided earlier in this guide to create an organized and aesthetically pleasing Plex Media Server library.

Further Reading

For more information on naming conventions and metadata handling in Plex, refer to the Plex Directory Naming Documentation.

Collaboration

If you have additional resources or scripts that help organize Plex media folders and want to collaborate, please share them in the comments or provide links to your Gists. Let's make media management easier together!

Credit

This guide was created with the assistance of ChatGPT. Thank you for reading!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment