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

With the file command I need to verify many files if they ASCII or other format

Sometimes I get from file command:

  file1: ASCII English text

And sometimes I get different answer from file command

  file2: Non-ISO extended-ASCII English text, with very long lines

I am really not sure if there are other answers with different syntax

My question is:

I write the follwing ksh syntax to verify if file is a ASCII but I not sure if the

following syntax is the optimal syntax in order to verify ASCII format?

   [[ ` file  $some_file | grep –c ASCII ` = 1 ]] && print "you have ascii file for sure"

If someone have other suggestion to verify ASCII format for sure!, I will very glad to see that

share|improve this question
ASCII? In the days of internet and Unicode? You must be joking. – grawity Oct 26 '10 at 22:23
You do realize that file is a heuristic guess and not a guarantee, right? yes | head -c $((2**20)) > blah; dd if=/dev/urandom bs=1 count=1024 >> blah; file blah says blah: ASCII text even though it's not. – ephemient Oct 27 '10 at 19:07
yes I am understand but what I need to do if I want to make selection of files type , what the best thing to do? , any idea? – jennifer Oct 27 '10 at 20:21
if LC_ALL=C grep -q '[^[:print:][:space:]]' file; then
    echo "file contains non-ascii characters"
    echo "file contains ascii characters only"
share|improve this answer
hi ephemient - please explain LC_ALL=C before the grep command , why? – jennifer Oct 26 '10 at 22:43
LC_ALL=C forces grep to treat [[:print:]] as the "printable ASCII" character class. Otherwise it means "printable <whatever your current locale is>", which may be non-ASCII. For example, most Linux boxes are set up with UTF-8 locales, in which case [[:print:]] would match non-ASCII character sequences that are valid UTF-8 printable characters. – ephemient Oct 26 '10 at 22:49
@jennifer: name=value command is the syntax for temporarily setting an environment variable, in this case LC_ALL, for a single command. Setting locale to C makes sure [[:print:]] only matches ASCII characters (and not accented characters from your language). – grawity Oct 26 '10 at 22:50
why I get "file contains non-ascii characters" for /etc/hosts , as you know hosts file is ASCII file? – jennifer Oct 26 '10 at 22:51
@jennifer: Fixed. Probably included a tab or something like that; I forgot [[:print:]] is [[:graph:] ] not [[:graph:][:space:]]. – ephemient Oct 26 '10 at 22:55

How about...

if file -ib "$file" | grep -Eqs '^text/plain(;|$)'; then
    echo "It's text/plain."

I don't know how common is --mime-type; if it's standard, use

if file -b --mime-type "$file" | grep -qs '^text/plain$'; then

Alternatively grep -qs '^text/' for any text type.

share|improve this answer

Since you're parsing the output with code I'd suggest using the -i option on file so it outputs MIME types instead human-friendly strings. The MIME type output is more regular and that makes it a little easier to deal with in code.

As for the output types a look at man file says that:

    Default list of magic numbers

    Default list of magic numbers, used to output  mime types
    when the -i option is specified.

Take a look at those files for all the MIME types it can report to determine which types you'll care about when parsing the output from file. I suspect all you'll care is that the MIME type starts with text/.

share|improve this answer

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.