Way of identifying plaintext file types in bash?

0

Is there any alternative to the file command that can be used to identify plaintext file types that don't have a magic number? E.g. something I can give a file to that contains JSON and I'll get back an answer like "JSON" instead of "ASCII text".

Failing that, are there any alternative magic files available that can make the file command behave like this?

big-o

Posted 2014-03-27T19:28:27.797

Reputation: 9

Plain-text files have no distinguishable identifiers or structure, because they're PLAIN TEXT. :) – Ƭᴇcʜιᴇ007 – 2014-03-27T19:41:39.603

1@techie007, linux has binary files and plain text files. For plain text files, there are various formats that they can hold (xml, html) and patterns that are discernible for scripts (perl, python, bash, etc). IDEs use these patterns to color code the lines and keywords in the files. The file command can tell some differences (it will return "POSIX shell script text executable" for a shell script, which is still technically a plain text file). – MaQleod – 2014-03-27T19:49:31.627

3@maQleod There's no way to identify a file type that has no recognizable structure (aka "format"), regardless of OS. Since plain text files inherently have no defined structure or format, they can't be recognized as such. At best all you can do is rule out all other known structures and then assume the file is plain text. – Ƭᴇcʜιᴇ007 – 2014-03-27T19:57:20.567

I know of no generally superior replacement for file nor better source of magic files. But, since there are so many types of files for which file is either unreliable or insufficiently detailed, I often end up cobbling together custom python scripts for the purpose. – John1024 – 2014-03-28T01:34:19.693

This raises the question why would you like to do this? What exactly is wrong with file? – Sami Laine – 2014-03-28T06:07:35.353

Answers

0

As explained by @techie007 there is no sane and efficient way to identify the "format" of a text file, but if you specifically want to test whether a file contains valid JSON you can probably use jq for this task since I guess it will return with an errorcode != 0 on invalid JSON input.

Adrian Frühwirth

Posted 2014-03-27T19:28:27.797

Reputation: 771