Skip to content

Instantly share code, notes, and snippets.

@gserrano
Created May 10, 2026 13:58
Show Gist options
  • Select an option

  • Save gserrano/af974bd932a0503f343623da529bb4c1 to your computer and use it in GitHub Desktop.

Select an option

Save gserrano/af974bd932a0503f343623da529bb4c1 to your computer and use it in GitHub Desktop.
Organize folder into A-Z sub-folders
#!/bin/bash
# NOTE: This script requires Bash.
# This script organizes subdirectories into A-Z parent folders.
# 1. List available directories (excluding single letters and Plex Media Server)
echo "Available media directories to organize:"
options=()
while IFS= read -r d; do
dir="${d%/}"
# Ignore single letter dirs, the script itself, and Plex Media Server
if [[ "$dir" != "Plex Media Server" && ${#dir} -gt 1 && "$dir" != "organize_media_alphabetically.sh" ]]; then
options+=("$dir")
fi
done < <(ls -d */ 2>/dev/null)
if [ ${#options[@]} -eq 0 ]; then
echo "No suitable directories found."
exit 1
fi
# 2. Show menu
PS3="Select a directory to organize (or type 'q' to quit): "
select choice in "${options[@]}"; do
if [[ "$REPLY" == "q" ]]; then
exit 0
elif [ -n "$choice" ]; then
TARGET_DIR="$choice"
break
else
echo "Invalid selection."
fi
done
echo "Organizing: $TARGET_DIR"
cd "$TARGET_DIR" || exit 1
# 3. Organize logic
for dir in */; do
original="${dir%/}"
# Skip if not a directory or if it's a single letter
if [ ! -d "$original" ] || [ ${#original} -eq 1 ]; then
continue
fi
# Determine normalization (case-insensitive "The " check)
if [[ "$original" =~ ^[Tt][Hh][Ee]\ ]]; then
normalized="${original:4}"
else
normalized="$original"
fi
# Get the first character and uppercase it
first_char="${normalized:0:1}"
target_parent=$(echo "$first_char" | tr '[:lower:]' '[:upper:]')
if [[ "$target_parent" =~ [A-Z] ]]; then
mkdir -p "$target_parent"
echo "Moving '$original' to '$target_parent/'"
mv "$original" "$target_parent/"
fi
done
echo "Done organizing $TARGET_DIR."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment