7

This question already has an answer here:

First thing I noticed when switched from Windows to Linux was, that Linux has no strict naming convention and no obligatory file name extensions like .bmp, .jpg, .exe etc. Therefore I can not tell by the file name itself its file format.

If all JPEG files on my file system had the .jpg extension, I could simply find all JPEG files by:

find / -type f -name "*.jpg"

But if it is not the case I am clueless how to find all JPEG files.

marked as duplicate by jasonwryan, Anthon, Networker, don_crissti, Gilles command-line Mar 25 '15 at 23:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Isn't this the same question as this one ? – don_crissti Mar 25 '15 at 19:47
  • No its not the same one. Here he is asking about the cases where the files have NO EXTENSION OF JPG – Milind Dumbare Mar 25 '15 at 19:53
  • @Miline - the other question (same poster) asks " for a method to find only JPEG files" it doesn't say anything about the files actually having the extension .jpg so it's pretty much the same question. – don_crissti Mar 25 '15 at 19:59
  • he he.. yes looks like.. and even the person asking is same. – Milind Dumbare Mar 25 '15 at 20:01
  • 1
    frankly speaking, i know realize (after learning some linux here), that my wording of the question was bad, by the way its focus is different from this even if the answer is contained in the other as a subset of a wider explanation. – Abdul Al Hazred Mar 25 '15 at 20:06
16

If you want to crawl on dirs and subdirs:

find /home/place/to/crawl -type f -exec file --mime-type {}  \; | awk '{if ($NF == "image/jpeg") print $0 }'

What it does?

  • Search all inodes with the type file
  • Execute the command file, to get a jpeg header of the file like: image/jpeg
  • awk

Edit: Added @Franklin tip, to use file with -i to use the mime string standard while outputing filetypes. This will reduce the false positives of the jpeg word.

Edit2: Added @don_crissti tip. Filtering now just the last column with awk and printing the whole line if matches with image/jpeg. Changed the file switch to --mime-type to suppress charset information

  • very helpful info that "file header reader", i read the headers of a jpg , gif and png file with the file command and all had the word "image" in them, does this mean that if i exchanged "...| grep JPEG" with "...| grep image" that all images regardless of format would be found ? – Abdul Al Hazred Mar 25 '15 at 20:11
  • 1
    @AbdulAlHazred: Yes, it means that. grep is a tool that filters lines of text that contain a certain substring. If you grep JPEG some text, you'll get only the lines containing "JPEG". If you grep image some text, you'll get only the lines containing "image". – mgarciaisaia Mar 25 '15 at 20:13
  • 1
    Not all image formats. BPM for example is an exception, an you shall find something like: PC bitmap, Windows 3.x format, 3264 x 2448 x 24. You will get almost all image format headers this way, but, you will have to deal with the black sheep, as the .bmp format has shown ;) – user34720 Mar 25 '15 at 20:14
  • 1
    Fixed. Greping JPEG image data should do the trick – user34720 Mar 25 '15 at 20:23
  • 3
    using file -i prints MIME type like image/jpeg. That's easier and more reliable to grep since mime type are guaranteed to never change. Also, it's easy to list all/any images format. Example to list all JPEG images: find /home/dir/example -type f -exec file -i {} \; | grep ': image/jpeg\>' – Franklin Piat Mar 25 '15 at 20:52

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