Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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?

share|improve this answer
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 '10 at 9:03
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.