Command Line Basics: Determine File MIME Types

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:

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

One thought on “Command Line Basics: Determine File MIME Types

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.