4

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?

  • Do you have a reason to suspect that some JPEG files don't have a .jpg extension (or vice versa)? If not, search by name, as in sam's answer. – Scott Feb 19 '15 at 21:22
7

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"
' {} \;
7

As a start, you could print out files with a .jpg file extension with:

sudo find / -name *.jpg -print

See how that behaves, modify to suit, and you can then pipe the output into another function rather than just printout if you'd like. For example, if you want to allow .JPG as well:

sudo find / -iname "*.jpg"
  • 3
    *.jpg should be "*.jpg", or it's expanded by the shell and won't do what you are trying to do. The -print isn't necessary in this case since nothing else is going on. – goldilocks Feb 19 '15 at 20:43
  • 1
    I would recommend: (1) Use -iname rather than -name, so you'll find *.JPG, too. (2) Search for the other extensions that JPEG files sometimes have, like .jpeg, .jpe, and .jfif. – Scott Feb 19 '15 at 21:26
  • True the -print is redundant in this specific case. This was just an quick place to get him started. I'm not sure about the " however, as it returns the same results for me with/without. Maybe you could elucidate the implications. – sam Feb 19 '15 at 21:27
  • You do not need sudo unless you are looking for private stuff (in other peoples directories), though you will need a 2>/dev/null. – ctrl-alt-delor Feb 19 '15 at 23:18
  • 1
    @sam without " quotes the *.jpg will match files in your current directory. While there aren't any the pattern will remain *.jpg but as soon as you have at least one the pattern will be replaced by the matching list. Quote the pattern to prevent this expansion. – roaima Feb 20 '15 at 0:20
0

This will print the paths to all files with the mime-type of image/jpeg.

find / -type f -exec file --no-pad --mime-type {} + 2>/dev/null \
    | awk '$NF == "image/jpeg" {$NF=""; sub(": $", ""); print}'

A few things to note:

  • use -exec command {} + instead of -exec command {} \; so as to only call file once (instead of once for every file)
  • use the --no-pad option to ensure file's output is in a consistent format (to make parsing it easier)
  • use 2>/dev/null to suppress stderr of find, only necessary because running the command from / will result in numerous 'Permission denied' errors that will clutter the console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.