#!/bin/bash VERSION=1.5 #NO_COLOR=1 # Check if NO_COLOR is set to disable colorization if [ -z "$NO_COLOR" ]; then : "${COLOR_GREEN:=$'\e[32m'}" : "${COLOR_GREEN_BOLD:=$'\e[1;32m'}" : "${COLOR_RED:=$'\e[31m'}" : "${COLOR_RED_BOLD:=$'\e[1;31m'}" : "${COLOR_YELLOW=$'\e[33m'}" : "${COLOR_YELLOW_BOLD:=$'\e[1;33m'}" : "${COLOR_SILVER=$'\e[37m'}" : "${COLOR_LIGHT_GREY=$'\e[249m'}" : "${COLOR_BRIGHT_PURPLE=$'\e[35;1m'}" : "${COLOR_PURPLE=$'\e[35m'}" : "${BOLD:=$'\e[1m'}" : "${ITALIC:=$'\e[3m'}" : "${COLOR_RESET:=$'\e[00m'}" else : "${BOLD:=$'\e[1m'}" : "${COLOR_RESET:=$'\e[00m'}" : "${ITALIC:=$'\e[3m'}" fi fr_months=("Janvier" "Février" "Mars" "Avril" "Mai" "Juin" "Juillet" "Août" "Septembre" "Octobre" "Novembre" "Décembre") # Vérifiez si exiftool est installé if ! command -v exiftool &> /dev/null; then echo "exiftool n'est pas installé. Veuillez l'installer d'abord." exit 1 fi # Help Help() { clear echo -e "\n${COLOR_GREEN_BOLD}Sort Export Folder $VERSION${COLOR_RESET}\n" echo "Syntaxe: sort_export_folder.sh []" echo echo "Tri les photos contenues dans un dossier, et selon le mois de prise de vue, les copies dans des dossiers AN/Mois AN" echo "Requiert le tag -CreateDate avec exiftool" echo echo "Options:" echo " -h Affiche l'aide" echo " -d Dossier à trier" echo echo "Exemples: sort_export_folder.sh -d /Volumes/Sophie/Export" echo " sort_export_folder.sh" echo echo "Requiert l'installation de exiftool" echo " -https://exiftool.org" echo " -brew install exiftool" } ### Parse options optspec="fh" while getopts "$optspec" opt do case ${opt} in d) PHOTO_DIR="${!OPTIND}"; OPTIND=$(( OPTIND + 1 )) ;; h) Help Help exit 2 ;; *) exit 1 ;; esac done shift $((OPTIND -1)) if [[ -z "$PHOTO_DIR" ]]; then # Répertoire contenant les photos while : do read -e -p "Répertoire contenant les photos: " PHOTO_DIR [ "$PHOTO_DIR" == "q" ] && exit 1 if [ "$PHOTO_DIR" != "q" ] && [ -d "$PHOTO_DIR" ]; then break 2 fi done fi nb_jpg_before=$(ls -Uba1 "$PHOTO_DIR" | grep -E -c '.jpg|.jpeg|.cr3|.tif|.dng|.webp|.avif'$) echo "$nb_jpg_before images trouvées dans $PHOTO_DIR" [ $nb_jpg_before -eq 0 ] && exit 3 echo i=0 # Parcourir chaque fichier dans le répertoire for PHOTO in "$PHOTO_DIR"/*; do # Vérifiez si le fichier est une image if [[ -f "$PHOTO" ]] && [[ $(file --mime-type -b "$PHOTO") == image/* ]]; then # Extraire la date de création EXIF DATE=$(exiftool -d "%Y-%m" -CreateDate -s -s -s "$PHOTO" 2>/dev/null) if [[ -n "$DATE" ]]; then y=$(echo "$DATE" | awk -F"-" '{print $1}') m=$(echo "$DATE" | awk -F"-" '{print $2}') else # image extraite d'une vidéo -> pas de -CreateDate # 2015-09-11_Daguet_0090.jpg x=$(basename "$PHOTO" | awk -F"_" '{print $1}') y=$(echo "$x" | awk -F"-" '{print $1}') m=$(echo "$x" | awk -F"-" '{print $2}') fi # Si le mois commence par 0, on supprime le 0 [ ${m:0:1} == "0" ] && m=${m:1} # L'index du tableau commence par 0, donc on retranche 1 d=${fr_months["$((m - 1))"]} month=$(echo "$d $y" | sed 'y/àáâäãåèéêëìíîïòóôöõùúûüÿćčçčďđłńňřśšťžż/aaaaaaeeeeiiiiooooouuuuyccccddlnnrsstzz/') # Si la date est extraite avec succès if [[ -n "$month" ]]; then # Créer le répertoire pour le mois correspondant MONTH_DIR="$PHOTO_DIR/$month" mkdir -p "$MONTH_DIR" # Déplacer la photo dans le répertoire du mois mv "$PHOTO" "$MONTH_DIR/" & #echo "Déplacé : $PHOTO -> $MONTH_DIR/" else echo "Aucune date EXIF trouvée pour : $PHOTO" fi ((++i)) fi done wait echo "Triage de $i photos terminé." nb_jpg_after=$(ls -Uba1 "$PHOTO_DIR" | grep -E -c '.jpg|.jpeg|.cr3|.tif|.dng|.webp|.avif'$) echo "$nb_jpg_after images trouvées dans $PHOTO_DIR" #subfolders=$(ls -A "$PHOTO_DIR" | tr '\n' ', ' | sed 's/.$//') subfolders=$(ls -A "$PHOTO_DIR" | tr '\n' ' ' | sed 's/.$//') ##### echo nb_subfolders=$(ls -l "$PHOTO_DIR" | grep -c ^d) echo "Il y a $nb_subfolders sous-dossiers dans $PHOTO_DIR. Rangeons-les par année !" for DIR in "$PHOTO_DIR"/*; do if [[ -d "$DIR" ]]; then year=$(basename "$DIR" | awk -F" " '{print $2}') new_year+=(${year}) if [[ -n "$year" ]]; then # Créer le répertoire pour l'année correspondante YEAR_DIR="$PHOTO_DIR/$year" mkdir -p "$YEAR_DIR" mv "$DIR" "$YEAR_DIR/" & #echo "Déplacé : $DIR -> $YEAR_DIR/" fi fi done nb_year_folders=$(ls -l "$PHOTO_DIR" | grep -c ^d) #year_folders=$(ls -d *) year_folders=$(cd "$PHOTO_DIR"; ls -d * | tr '\n' ' ') echo "$nb_year_folders sous-répertoires $year_folders crées." ### nb_jpg_id_dest=$(find "$PHOTO_DIR" -mindepth 2 -type f -exec bash -c 'echo ${0%${0#$1}}' {} "$PHOTO_DIR" \; | uniq -c | awk '{print $1}') echo "$nb_jpg_id_dest images trouvées dans les sous-répertoires $subfolders" [ $nb_jpg_after -ne 0 ] && echo "Erreur !! Il reste des photos dans le dossier d'origine." [ $nb_jpg_before -ne $nb_jpg_id_dest ] && echo "Erreur !! Toutes les photos n'ont pas été déplacées." echo tr=$(tree -d "$PHOTO_DIR" | head -n -1) echo -e "${COLOR_PURPLE}$tr${COLOR_RESET}" tree -a "$PHOTO_DIR" | tail -n 1