Skip to content

Instantly share code, notes, and snippets.

@theg00s3
Last active September 17, 2024 02:30
Show Gist options
  • Save theg00s3/61efee55d98e8a6dfc623a2870136a39 to your computer and use it in GitHub Desktop.
Save theg00s3/61efee55d98e8a6dfc623a2870136a39 to your computer and use it in GitHub Desktop.
Renames photo files that have the wrong extension
#!/bin/bash
# credit to u/nkings10's post on Reddit which was the template for this: https://www.reddit.com/r/photoprism/comments/1ev7zxm/import_errors/
# Directory containing images
IMAGE_DIR="/path/to/your/images"
# Find all files recursively and process each one
find "$IMAGE_DIR" -type f | while read -r file; do
# Detect the MIME type using `file`
mime_type=$(file --mime-type -b "$file")
# Determine the correct extension based on MIME type
case "$mime_type" in
image/jpeg)
new_ext="jpg"
;;
image/png)
new_ext="png"
;;
image/gif)
new_ext="gif"
;;
image/bmp)
new_ext="bmp"
;;
image/tiff)
new_ext="tiff"
;;
image/heic)
new_ext="heic"
;;
*)
echo "Skipping $file, unrecognized MIME type: $mime_type"
continue
;;
esac
if [ "${file##*.}" = $new_ext ]; then
echo "Skipping $file, already has correct extension: $new_ext"
continue
fi
# Extract the filename without the extension
base_name=$(basename "$file" | sed -e 's/\.[a-zA-Z0-9]*$//')
# Get the directory of the file
dir_name=$(dirname "$file")
# Rename the file
mv "$file" "$dir_name/$base_name.$new_ext"
echo "Renamed $file to $dir_name/$base_name.$new_ext"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment