How do I distinguish between 'binary' and 'text' files? How do I distinguish between 'binary' and 'text' files? unix unix

How do I distinguish between 'binary' and 'text' files?


You can use the file command. It does a bunch of tests on the file (man file) to decide if it's binary or text. You can look at/borrow its source code if you need to do that from C.

file READMEREADME: ASCII English text, with very long linesfile /bin/bash/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped


The spreadsheet software my company makes reads a number of binary file formats as well as text files.

We first look at the first few bytes for a magic number which we recognize. If we do not recognize the magic number of any of the binary types we read, then we look at up to the first 2K bytes of the file to see whether it appears to be a UTF-8, UTF-16 or a text file encoded in the current code page of the host operating system. If it passes none of these tests, we assume that it is not a file we can deal with and throw an appropriate exception.


You can determine the MIME type of the file with

file --mime FILENAME

The shorthand is file -i on Linux and file -I (capital i) on macOS (see comments).

If it starts with text/, it's text, otherwise binary. The only exception are XML applications. You can match those by looking for +xml at the end of the file type.