Shell – How to list only JPEG files from root below using the command line

file-commandfindshell-script

I am searching for a method to find only JPEG files/
With my limited knowledge of Linux I came to this point:

  1. list all paths that exist from root below with find /
  2. pipe the result into next command and perform the file command on each found path with xargs file
  3. In the result of the file command is a JPEG string contained, I thought maybe it would be possible somehow to an IF-statement to filter only the JPEGs: If (JPEG contained in output of file command) {show argument of file}

once more:

find / | xargs file | "If statement" 

Could you please correct me, give me a hint how to perform the task or give a solution?

Best Answer

If you want to list files containing a JPEG image regardless of the extension in the filename, you could use find + file to list the files with mime type image/jpeg:

find . -type f -exec sh -c '
    file --mime-type "$0" | grep -q image/jpeg\$ && printf "$0\n"
' {} \;

or

find . -type f -exec sh -c '
    mt=$(file --brief --mime-type "$0")
    [ -z "${mt#image/jpeg}" ] && printf "$0\n"
' {} \;
Related Question