Skip to content

Instantly share code, notes, and snippets.

@apetersson
Created October 1, 2025 13:47
Show Gist options
  • Select an option

  • Save apetersson/31391a1e58f15e28603097179018c850 to your computer and use it in GitHub Desktop.

Select an option

Save apetersson/31391a1e58f15e28603097179018c850 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Startverzeichnis als Parameter
if [ -z "$1" ]; then
echo "Verwendung: $0 <Startverzeichnis>"
exit 1
fi
start_dir="$1"
# Funktion zur Umwandlung eines Datumsformats in Unix-Zeit (Epoch)
date_to_epoch() {
local date="$1"
echo "Umwandlung in Epoch: $date" # Debug-Ausgabe
date --date="$date" +"%s"
}
# Funktion zum Überprüfen der Übereinstimmung unter Berücksichtigung der Toleranz
check_date_match_with_tolerance() {
local file="$1"
local filename_date="$2"
local exif_date="$3"
local tolerance="$4"
echo "Überprüfung: Dateiname-Datum: $filename_date, EXIF-Datum: $exif_date, Toleranz: $tolerance Sekunden" # Debug-Ausgabe
# Konvertiere die Daten in Epoch-Zeit
local filename_epoch=$(date_to_epoch "$filename_date")
local exif_epoch=$(date_to_epoch "$exif_date")
# Berechne die Differenz
local diff=$((filename_epoch - exif_epoch))
echo "Zeitdifferenz: $diff Sekunden" # Debug-Ausgabe
# Prüfe, ob die Differenz innerhalb der Toleranz liegt
if [ "$diff" -le "$tolerance" ] && [ "$diff" -ge "-$tolerance" ]; then
echo "Übereinstimmung mit Toleranz: $file - $filename_date und $exif_date"
else
echo "Nicht übereinstimmend: $file - Dateiname: $filename_date, EXIF: $exif_date"
fi
}
# Funktion zur Umwandlung des Datums aus dem Dateinamen
#convert_filename_to_date() {
# local filename="$1"
#
# # Extrahiert Datum, Uhrzeit und Toleranz basierend auf bekannten Mustern
# if [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)_(\[0-9\]{4})(\[0-9\]{2})(\[0-9\]{2})[-_](\[0-9\]{2})(\[0-9\]{2})(\[0-9\]{2})\\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# # Format: YYYY-MM-DD HH:MM:SS
# local date="${BASH_REMATCH[2]}-${BASH_REMATCH[3]}-${BASH_REMATCH[4]}"
# local time="${BASH_REMATCH[5]}:${BASH_REMATCH[6]}:${BASH_REMATCH[7]}"
# local tolerance=600
# echo "$date $time $tolerance"
# elif [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)[-_](\[0-9\]{8})[-_]\\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# # Format: YYYYMMDD
# local date="${BASH_REMATCH[2]:0:4}-${BASH_REMATCH[2]:4:2}-${BASH_REMATCH[2]:6:2}"
# local time="00:00:00"
# local tolerance=86400
# echo "$date $time $tolerance"
# elif [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)[-_](\[0-9\]{4})(\[0-9\]{2})(\[0-9\]{2})[-_]\\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# # Format: YYYY-MM-DD (WhatsApp)
# local date="${BASH_REMATCH[2]}-${BASH_REMATCH[3]}-${BASH_REMATCH[4]}"
# local time="00:00:00"
# local tolerance=86400
# echo "$date $time $tolerance"
# else
# echo ""
# fi
#}
# Funktion zur Umwandlung des Datums aus dem Dateinamen
convert_filename_to_date() {
local filename="$1"
# Extrahiert Datum, Uhrzeit und Toleranz basierend auf bekannten Mustern
if [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)_(\d{4})(\d{2})(\d{2})[-_](\d{2})(\d{2})(\d{2})\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# Format: YYYY-MM-DD HH:MM:SS
local date="${BASH_REMATCH[2]}-${BASH_REMATCH[3]}-${BASH_REMATCH[4]}"
local time="${BASH_REMATCH[5]}:${BASH_REMATCH[6]}:${BASH_REMATCH[7]}"
local tolerance=600
echo "$date $time $tolerance"
elif [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)[-_](\d{8})[-_]\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# Format: YYYYMMDD
local date="${BASH_REMATCH[2]:0:4}-${BASH_REMATCH[2]:4:2}-${BASH_REMATCH[2]:6:2}"
local time="00:00:00"
local tolerance=86400
echo "$date $time $tolerance"
elif [[ $filename =~ ^(IMG|VID|PANO|Screenshot|PXL)[-_](\d{4})(\d{2})(\d{2})[-_]\.(jpg|jpeg|png|mp4|3gp|MP)$ ]]; then
# Format: YYYY-MM-DD (WhatsApp)
local date="${BASH_REMATCH[2]}-${BASH_REMATCH[3]}-${BASH_REMATCH[4]}"
local time="00:00:00"
local tolerance=86400
echo "$date $time $tolerance"
else
echo ""
fi
}
# Dateien finden und bearbeiten
find "$start_dir" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.mp4" -o -iname "*.3gp" \) | sort | while read file; do
filename=$(basename "$file")
echo "checking $filename"
# Datum, Uhrzeit und Toleranz aus Dateinamen umwandeln
file_date_and_time=$(convert_filename_to_date "$filename")
echo "file_date_and_time $file_date_and_time"
if [ -n "$file_date_and_time" ]; then
# Datum, Uhrzeit und Toleranz aus Dateinamen extrahieren
IFS=" " read -r file_date file_time tolerance <<< "$file_date_and_time"
# Datum aus EXIF-Daten extrahieren
exif_date_raw=$(exiftool -DateTimeOriginal -s3 "$file" | tr -d ' ')
# Korrektur für das Format des EXIF-Datums
exif_date=$(echo "$exif_date_raw" | sed -r 's/^(\[0-9\]{4}):(\[0-9\]{2}):(\[0-9\]{2})(\[0-9\]{2}):(\[0-9\]{2}):(\[0-9\]{2})$/\1-\2-\3 \4:\5:\6/')
# Vergleichen mit Toleranz
check_date_match_with_tolerance "$file" "$file_date $file_time" "$exif_date" "$tolerance"
else
echo "unable to extract date from $filename"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment