#!/opt/homebrew/bin/bash FILE="$1" PREFIX=$(date +%Y-%m-%d) # 1. Basic error checking if [ -z "$FILE" ]; then echo "Usage: date-prefix " exit 1 fi if [ ! -f "$FILE" ]; then echo "Error: '$FILE' not found or is not a file." exit 1 fi # 2. Extract extension and base filename # This splits "photo.jpg" into EXT=".jpg" and BASE="photo" EXT="${FILE##*.}" BASE="${FILE%.*}" # Handle files with no extension (where BASE and EXT would be the same) if [ "$EXT" = "$BASE" ]; then EXT="" else EXT=".$EXT" fi # 3. Determine the target name TARGET="${PREFIX}-${BASE}${EXT}" # 4. If the target exists, find the next available increment if [ -e "$TARGET" ]; then COUNTER=2 # New logic: Check for 2026-04-13-image-2.png while [ -e "${PREFIX}-${BASE}-${COUNTER}${EXT}" ]; do ((COUNTER++)) done TARGET="${PREFIX}-${BASE}-${COUNTER}${EXT}" fi # 5. Perform the move mv "$FILE" "$TARGET" echo "Renamed: $FILE -> $TARGET"