Skip to content

Instantly share code, notes, and snippets.

@coltenkrauter
Last active September 14, 2023 00:29
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/eecc88aa0ef7bd9ffa739e3bba1efa9f to your computer and use it in GitHub Desktop.
Save coltenkrauter/eecc88aa0ef7bd9ffa739e3bba1efa9f to your computer and use it in GitHub Desktop.
This script helps in maintaining a clean and organized directory structure for Plex servers or similar setups. By identifying any nested directories that share the same name with their parent or grandparent directories, it aids in ensuring that automated tools and scripts function idempotently, preventing potential errors due to duplicate nested…

Directory Match Finder

In managing a robust Plex server, overseeing a large and complex directory structure is a given. Ensuring that your automated organization tools and scripts operate seamlessly is vital. This script is crafted to be your handy tool in this process, helping you identify directories that share the name with their parent or grandparent directories quickly and efficiently. Below is a user-friendly guide and the script to facilitate this.

Table of Contents

Script Implementation

To simplify your work, the script below performs a thorough, recursive check from your current directory. It identifies any nested directories that have the same name as their parent or grandparent directories. If a match is found, the directory path will be printed, keeping you informed of any redundancies. Here's the script ready for you to copy:

#!/bin/zsh

# Define styles and colors
BLUE='\033[0;34m'
GREEN='\033[0;32m'
MAGENTA='\033[0;35m'
BOLD='\033[1m'
NC='\033[0m' # Reset to no color

# Initialize counter for the final report
count=0

# Initiate with a bold blue header that includes script name and lines to signify commencement
echo -e "${BLUE}${BOLD}═════ Directory Matching Script ═════${NC}"

# Use green to mark the start phase, delineating progress
echo -e "${GREEN}${NC} Script is now running...\n"

# Begin the directory checking process
find . -type d -print | while IFS= read -r dir; do
    dir_name="$(basename "$dir")"
    current_dir="$dir"
    
    while [[ "$current_dir" != "." ]]; do
        parent_dir="$(dirname "$current_dir")"
        parent_dir_name="$(basename "$parent_dir")"
        
        if [[ "$dir_name" == "$parent_dir_name" && "$dir_name" != "." ]]; then
            # Employ magenta arrows and bolded paths for easy spotting
            echo -e "   ${MAGENTA}${NC} Detected match: ${BOLD}$dir${NC}"
            count=$((count+1))
            break
        fi
        
        current_dir="$parent_dir"
    done
done

# Conclude with a blue-bolded section for a concise end report
echo -e "\n${BLUE}${BOLD}═════ End Report ═════${NC}"
echo -e "${GREEN}${NC} Script completed. Total matches found: ${BOLD}$count${NC}\n"

# Reset styles periodically to ensure a streamlined output
echo -e "${NC}"

How to Use

Using the script is a breeze. Just save it in a file named directory-match-finder.zsh, then run chmod +x directory-match-finder.zsh to grant executable permissions, and finally, initiate it with ./directory-match-finder.zsh in your terminal.

Use Case

Your Plex server is a hub of media and organizing it proficiently is crucial. Tools such as FileBot and MP3Tag alongside scripts like this helpful one have been key in maintaining a well-organized directory structure.

This script steps in as a safeguard, ensuring that none of these tools or scripts have inadvertently created duplicate nested directories - a sign that a prior operation wasn't idempotent. With this script, you can easily verify the integrity of your directory structure, avoiding potential hiccups in the future.

Resources

To deepen your understanding or enhance your skills, consider these resources:

  1. Linux Find Command Documentation
  2. Zsh Manual
  3. Shell Scripting Tutorial
  4. YouTube Tutorial: Recursive Scripting

Acknowledgements

This script was developed with guidance from GPT-4 by OpenAI.

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