2

I am looking a Bash command that will allow me to identify all images in a directory regardless of file extension. For example if a photo was given a .doc extension I want to be able to identify it using a script.

So far I have the below but it is only returning images with the typical file extensions.

find . -type f -exec file {} \; | grep -i -o -E '^.+: \w+ image' 
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
chris
  • 21
  • 1
  • 2
    This works for me. `find . -maxdepth 1 -type f -exec file {} \; | grep -i -o -E '^.+: \w+ image' ` returns `./pic.doc: PNG image ./test.png: PNG image` – jeremysprofile Jun 13 '18 at 20:11
  • 1
    Possible duplicate of [How to get magic number of a binary file](https://stackoverflow.com/q/2147484/608639), [Detect file-type with the help of magic-file and rename file-suffix accordingly](https://unix.stackexchange.com/q/43544/56041), [`magic(5)` man page](https://linux.die.net/man/5/magic), etc. – jww Jun 14 '18 at 00:26

1 Answers1

3

I would go with this:

file --mime-type * | grep "image/"

Sample Output

gif.doc:             image/gif
jpg.doc:             image/jpeg
png.doc:             image/png
result.doc:          image/tiff
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432