File command apparently returning wrong MIME type

file-commandmime-types

Why doesn't the following return text/csv?

$ echo 'foo,bar\nbaz,quux' > temp.csv;file -b --mime temp.csv
text/plain; charset=us-ascii

I used this example for extra clarity but I'm also experiencing the problem with other CSV files.

$ file -b --mime '/Users/jasonswett/projects/client_work/gd/spec/test_files/wtf.csv'
text/plain; charset=us-ascii

Why doesn't it think the CSV is a CSV? Is there anything I can do to the CSV to make file return the "right" thing?

Best Answer

Unfortunately, there is probably nothing you can do to make file produce the correct output.

The file command tests the first few bytes of a file against a database of magic numbers. That is easy to check for in binary files (like images or executables) which have some specific identifiers at the beginning of the file.

If the file is not a binary file, it will check the encoding as well as look for some specific words in the file to determine the type, but only for a limited number of file types (most of which are programming languages).

Related Question