file
(in Ubuntu anyway)
supports the -r
and -0
options which I think could be used to safely extract filenames from its output. So the filenames could be passed into
file
, the file types checked, and the filenames that appear to be JPEGs could be taken from that same output, safely because they are null-character-terminated with all original weird characters intact before the terminator, and operated on.