Skip to content

Instantly share code, notes, and snippets.

@joelanman
Created March 13, 2026 20:14
Show Gist options
  • Select an option

  • Save joelanman/bd4777867058b60ab2ebc6773e90ef1c to your computer and use it in GitHub Desktop.

Select an option

Save joelanman/bd4777867058b60ab2ebc6773e90ef1c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to organize photos by month using copies
# Source: ~/Documents/Photos/YYYY_MM_DD/exports/
# Destination: ~/Documents/Photos/Monthly/YYYY-MM/
SOURCE_DIR="$HOME/Documents/Photos"
MONTHLY_DIR="$SOURCE_DIR/Monthly"
# Create the monthly directory if it doesn't exist
mkdir -p "$MONTHLY_DIR"
echo "Organizing photos by month..."
echo "Source: $SOURCE_DIR"
echo "Destination: $MONTHLY_DIR"
echo ""
# Find all day folders, sort by folder name (YYYY_MM_DD), and process them in order
find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -type d | sort | while read -r day_folder; do
# Extract the date from folder name (expects YYYY_MM_DD format)
folder_name=$(basename "$day_folder")
# Skip if it's the Monthly directory we're creating
[[ "$folder_name" == "Monthly" ]] && continue
# Check if exports folder exists
exports_folder="$day_folder/exports"
if [[ ! -d "$exports_folder" ]]; then
continue
fi
# Extract year and month from folder name (YYYY_MM_DD)
if [[ "$folder_name" =~ ^([0-9]{4})_([0-9]{2})_[0-9]{2}$ ]]; then
year="${BASH_REMATCH[1]}"
month="${BASH_REMATCH[2]}"
# Create monthly folder name (YYYY-MM)
month_folder="$MONTHLY_DIR/$year-$month"
# Create the monthly folder if it doesn't exist
mkdir -p "$month_folder"
# Copy all files from exports to the monthly folder
find "$exports_folder" -maxdepth 1 -type f | while read -r file; do
filename=$(basename "$file")
target="$month_folder/$filename"
# Only copy file if it doesn't already exist
if [[ ! -e "$target" ]]; then
cp "$file" "$target"
fi
done
echo "Processed: $folder_name (YYYY_MM_DD) -> $month_folder"
else
echo "Skipping (unexpected folder name, expected YYYY_MM_DD): $folder_name"
fi
done
echo ""
echo "Done! Photos organized in: $MONTHLY_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment