The linux file command does a very good job in recognising file types and gives very fine-grained results. The diff tool is able to tell binary files f
file
diff
Commands like less, grep detect it quite easily(and fast). You can have a look at their source.