How can I tell if a file is a binary file or a text file without opening it?

2 Answers 2


There's no way to verify what kind of contents a file has without some program opening it and actually reading the contents.

If you're asking how to tell whether the file is binary or text without opening it in a text editor, that's certainly possible, at least for most files. Linux includes the file program for just this purpose; at the command line, run

file name_of_file

(where of course you replace name_of_file with the actual name of the file you're testing). The file program will print out an informed guess at the type of the file, based on its contents, and it's usually pretty good at guessing.

Of course, there will always be some corner cases where even file can't tell. If a file is UTF-8 encoded text that includes a lot of high-numbered Unicode characters, for example, it'd probably look like a binary file and you'd get gibberish if you open it in a text editor that isn't Unicode-aware. Although even in that case, I think file might be able to identify it correctly... the point is, sometimes it's hard/impossible to tell at all, so you can never be 100% sure that you've identified the contents type correctly.

I guess the bigger question would be, why are you trying to distinguish binary files from text files?

  • 1
    You may want to use file -b -i name_of_file, which prints out mime-type[ mime-type-options] formatted output which is suitable for using in scripts. see man 1 file
    – Notinlist
    Sep 7, 2010 at 9:03

This is impossible. All you can do is use tools like file to make a best guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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