Get File MIME Type from Command Line

By  on  

I've gotten skilled at shell scripting over the years. I love a good GUI but knowing how to automate makes you a much more powerful engineer. Much of my scripting requires recursing over directories and processing a file if it meets a given criteria, which is often file extension or MIME type.

You can use the following shell command to get a file's MIME type:

file --mime-type -b Downloads/main.js
# text/plain

file --mime-type -b Downloads/logo.jpg
# image/jpeg

It's important to use the brief (-b) option in the command or you may receive an error message.

MIME type is used for validation and any number of other informational use cases. Luckily the file command and and a flag is all you need!

Recent Features

Incredible Demos

  • By
    Implement jQuery’s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...

  • By
    Ana Tudor’s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!