10

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.

10
  • Isn't this the same question as this one ? Commented Mar 25, 2015 at 19:47
  • No its not the same one. Here he is asking about the cases where the files have NO EXTENSION OF JPG Commented Mar 25, 2015 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. Commented Mar 25, 2015 at 19:59
  • he he.. yes looks like.. and even the person asking is same. Commented Mar 25, 2015 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. Commented Mar 25, 2015 at 20:06

1 Answer 1

20

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

9
  • 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". Commented Mar 25, 2015 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
    Commented Mar 25, 2015 at 20:14
  • 1
    Fixed. Greping JPEG image data should do the trick
    – user34720
    Commented Mar 25, 2015 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\>' Commented Mar 25, 2015 at 20:52
  • 1
    I strongly recommend use of -exec command {} + instead of -exec command {} \; so as to only call file once (instead of once for every file). It will save a lot of time. You may want to combine this with file's --no-pad option. The completed command would look something like this: find -type f -exec file --no-pad --mime-type {} + | awk '$NF == "image/jpeg" {$NF=""; sub(": $", ""); print}'. This only prints filenames only, does not include any extraneous output per request of @don_crissti
    – Six
    Commented Sep 22, 2016 at 4:11

Not the answer you're looking for? Browse other questions tagged .