Command Line Basics: Determine File MIME Types

August 18, 2009 by
Filed under: command line, HowTo, linux 

I recently wrote a script to watermark all of the image files in a directory. Most of the images I use on this website are PNG files, but I occasionally use JPGs. The challenge in my script was to find all of the image files in the directory regardless of image type.

After a bit of web research I discovered the file command. The file command takes a list of files as input and outputs the file names along with file type.

For example, to see the file types of the files in your home directory, open a terminal and enter:

file *

To see the file types expressed as MIME types enter:

Affiliate Link
file -i *

For my image watermarking script I pipelined the output to the grep command like this to find all of the image files.

file -i * | grep image

The great thing about this command is that it will find all of your desired files regardless of file extension. For example, if you renamed a bunch of JPGs to eliminate the .jpg extension, you could still find all of them with

file -i * | grep image/jpeg

Comments

One Response to “Command Line Basics: Determine File MIME Types”

  1. dr1v3rloaded says:

    Just what i was looking for... thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>