Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently writing a shell script that needs to be able to identify the actual type of files (not their extensions) with the unix "file" utility. However, that tool outputs human readable text which might be somewhat difficult to parse. Before I write something that just matches specific substrings found in its output, is there a better way to approach this problem?

share|improve this question
To identify the character set used within files, file should suffice e.g. one way to parse out the character set: raw=$(file -i $1); cset=${raw#*=}. – user111228 Jan 7 '12 at 20:24
up vote 2 down vote accepted

Using the --mime-type argument to file gives MIME type which is easier to parse.

$ file --mime-type /etc/passwd
/etc/passwd: text/plain

The -i option gives a little bit more information (encoding type) if needed:

$ file -i /etc/passwd
/etc/passwd: text/plain; charset=us-ascii
share|improve this answer
Thanks, this will do perfectly for my script! – camperdave Jan 7 '12 at 20:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.