#!/bin/sh

image_ext='png|gif|jpe?g$'

for i in *; do
	type=$(file -b --mime-type "$i" | grep -Eo "/$image_ext/" | tr -d '/' | sed -e 's/jpeg/jpg/')

	# Add respective file-extension if needed.
	[ $type ] && {
		mv $i $(echo $i | sed -Ee "s/\.($image_ext)//")".$type" || {
			tput setaf 9;
			>&2 printf 'Could not rename file "%s".\n' $(basename $i);
			tput sgr0;
		};
	};

done;

#!/bin/sh

image_ext='png|gif|jpeg|jpg$'

for i in *; do
	type=$(file -b --mime-type "$i" | grep -Eo "/$image_ext/" | tr -d '/' | sed -e 's/jpeg/jpg/')

	# Add respective file-extension if needed.
	[ $type ] && {
		mv $i $(echo $i | sed -Ee "s/\.($image_ext)//")".$type" || {
			tput setaf 9;
			>&2 printf 'Could not rename file "%s".\n' $(basename $i);
			tput sgr0;
		};
	};

done;

Fix incorrect reading of JPEG MIME-types
Fix incorrect reading of JPEG MIME-types

The regular expression used to identify JPEG images in MIME-type strings
was using a quantifier unsupported by the standard grep utility. Simpler
approach was to simply extend the list of alternations to cover both JPG
and JPEG.
