Here's a modified version of Mohit's script. Instead of deleting misnamed files, it non-destructively renames them.
It also swaps out the os.system() calls for subprocess calls which solves escaping issues regarding quotes in filenames.
import glob
import subprocess
import os
import re
import logging
import traceback
filelist=glob.glob("/path/to/*.jpg")
for file_obj in filelist:
try:
jpg_str = subprocess.check_output(['file', file_obj]).decode()
if (re.search('PNG image data', jpg_str, re.IGNORECASE)) or (re.search('Png patch', jpg_str, re.IGNORECASE)):
old_path = os.path.splitext(file_obj)
if not os.path.isfile(old_path[0]+'.png'):
new_file = old_path[0]+'.png'
elif not os.path.isfile(file_obj+'.png'):
new_file = file_obj+'.png'
else:
print("Found PNG hiding as JPEG but couldn't rename:", file_obj)
continue
print("Found PNG hiding as JPEG, renaming:", file_obj, '->', new_file)
subprocess.run(['mv', file_obj, new_file])
except Exception as e:
logging.error(traceback.format_exc())
print("Cleaning JPEGs done")