How to Determine File Type & Encoding from Command Line in Mac OS X

Aug 11, 2015 - 8 Comments

Determine file type and encoding type of a file in Mac OS X command line

Typically if you’re looking to determine the file type and encoding of an item, you can simply look at the file in the Mac Finder, check the file name extension, Get Info about the file, or even open it to quickly find out what the file is. Of course, that’s limited to the user friendly file system of Mac OS X, and there are occasions where it may be necessary to detect how a file is encoded or what a file type is from the command line, often with less obvious clues (or no clues at all) than a visible file extension.


If you’re in a situation where you need to figure out what a particular file is and how it’s encoded, you can use the ‘file’ command with the uppercase i flag to quickly see what the file is, and it’s character set.

How to Determine File Type / Encoding via Command Line on Mac

To try this yourself, launch the Terminal application and issue the proper syntax.

The syntax to determine file encoding type and file type in Mac OS (and from the linux command line as well) looks like the following:

file -I filename

Do note the flag is a capital ‘i’ and not a lowercase l. The output of the properly executed command will read like the following:

/Path/To/Filename: fileformat/filetype; charset=encoding

Let’s look at a few examples, first is checking a file which turns out to be an image:

file -I ~/Desktop/iphone-plus
/Users/Paul/Desktop/iphone-plus: image/jpeg; charset=binary

The file type is clearly shown as is the character set.

Again, with another file, which shows as an xml encoded as us-ascii:

file -I osxdaily.com.webloc
osxdaily.com.webloc: application/xml; charset=us-ascii

Another example which turns out to be a plain old text file:

file -I ~/Documents/diywatch
~/Documents/diywatch: text/plain; charset=us-ascii

And another example which turns out to be an executable binary application:

file -I /usr/sbin/streamy
/usr/sbin/streamy: application/octet-stream; charset=binary

This command line approach to determining file type and encoding can be helpful for many reasons, whether for usage in a script, for remote troubleshooting or maintenance with ssh, finding specific file types and file formats with the built-in search functions in Mac OS X, or even for your own purposes of determining what a mystery file is, what app to open it with, and perhaps what extension type it should have if it’s missing one.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

8 Comments

» Comments RSS Feed

  1. stellababy says:

    Perfect! So hard to find this info. I have 50GB of mixed file types on a disk that have no extension.

    How can I search for multiple files by file type eg. “image/jpeg” and then add.jpg to their filenames?

  2. Peter Parker says:

    Andrea, I suggest GraphicConverter, because it supports a lot of old fileformats, that the file command may not recognize.

  3. Peter Parker says:

    Andrea, it might be best to grab a copy of GraphicConverter, it’s on the app store. It supports so many *old* file formats, that I assume it’s your best bet.

  4. Andrea Cowen says:

    I followed your instructions, got:
    application/octet-stream; charset=binary.
    Now what app do I use to open it or what extension do I add to the file name? These are image files from 1996 on a Mac.

    • gnano says:

      Try using GraphicConverter as Peter Parker recommended, it is an old Mac app that is still around and is able to open many different file types.

      https://www.lemkesoft.de/en/products/graphicconverter/

      You can even try the trial version

      You could also drag them into the Preview app for Mac to see if it detects them and can open them.

      Do you recall the file type? From 1996 Mac era, maybe PICT format?

  5. Jimbo Francis says:

    This doesn’t show the right encodings for me. I tried it on UTF-8 and Shift JIS text files (with Japanese text) and it said US-ASCII.

  6. Brett Mx says:

    Great idea. You can also type file -I in Terminal first, then drag & drop the file into the Terminal window. That will type the path for you.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site