|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to create directory if not exists |
| 4 | +create_directory() { |
| 5 | + if [ ! -d "$1" ]; then |
| 6 | + mkdir -p "$1" || { echo "Error: Failed to create directory $1"; exit 1; } |
| 7 | + fi |
| 8 | +} |
| 9 | + |
| 10 | +# Function to move photos by creation date |
| 11 | +move_photos_by_date() { |
| 12 | + local file creation_date destination_folder |
| 13 | + # Check if any photo files exist |
| 14 | + if compgen -G '*.jpg' >/dev/null || compgen -G '*.ARW' >/dev/null || compgen -G '*.NEF' >/dev/null || compgen -G '*.ORF' >/dev/null || compgen -G '*.RW2' >/dev/null || compgen -G '*.RAF' >/dev/null || compgen -G '*.DNG' >/dev/null; then |
| 15 | + shopt -s nocaseglob # Enable case-insensitive globbing |
| 16 | + for file in *.jpg *.ARW *.NEF *.ORF *.RW2 *.RAF *.DNG; do |
| 17 | + if [ -f "$file" ]; then |
| 18 | + creation_date=$(stat -f "%SB" -t "%Y-%m-%d" "$file" 2>/dev/null) |
| 19 | + if [ -n "$creation_date" ]; then |
| 20 | + destination_folder="$creation_date" |
| 21 | + create_directory "$destination_folder" |
| 22 | + mv "$file" "$destination_folder/" || { echo "Error: Failed to move file $file"; exit 1; } |
| 23 | + else |
| 24 | + echo "Failed to get creation date for file: $file" |
| 25 | + fi |
| 26 | + fi |
| 27 | + done |
| 28 | + shopt -u nocaseglob # Disable case-insensitive globbing |
| 29 | + echo "Photos moved successfully by date." |
| 30 | + else |
| 31 | + echo "No photo files found." |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# Function to move photos by file format |
| 36 | +move_photos_by_format() { |
| 37 | + local jpg_files arw_files |
| 38 | + jpg_files=(*.jpg *.JPG) |
| 39 | + arw_files=(*.ARW *.NEF *.ORF *.RW2 *.RAF *.DNG) |
| 40 | + if [ ${#jpg_files[@]} -gt 0 ] || [ ${#arw_files[@]} -gt 0 ]; then |
| 41 | + create_directory "JPG" |
| 42 | + create_directory "RAW" |
| 43 | + mv "${jpg_files[@]}" JPG/ 2>/dev/null |
| 44 | + mv "${arw_files[@]}" RAW/ 2>/dev/null |
| 45 | + echo "Photos moved successfully by format." |
| 46 | + else |
| 47 | + echo "No photo files found." |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +# Main function |
| 52 | +main() { |
| 53 | + echo "Welcome to Photo Organizer!" |
| 54 | + echo "1. Organize photos by date" |
| 55 | + echo "2. Organize photos by format" |
| 56 | + echo "3. Quit" |
| 57 | + read -p "Please select an option (1/2/3): " option |
| 58 | + case $option in |
| 59 | + 1) |
| 60 | + move_photos_by_date |
| 61 | + ;; |
| 62 | + 2) |
| 63 | + move_photos_by_format |
| 64 | + ;; |
| 65 | + 3) |
| 66 | + echo "Exiting..." |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "Invalid option. Please select again." |
| 70 | + ;; |
| 71 | + esac |
| 72 | +} |
| 73 | + |
| 74 | +# Call the main function |
| 75 | +main |
0 commit comments