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.
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}"
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.
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.
To deepen your understanding or enhance your skills, consider these resources:
- Linux Find Command Documentation
- Zsh Manual
- Shell Scripting Tutorial
- YouTube Tutorial: Recursive Scripting
This script was developed with guidance from GPT-4 by OpenAI.