Determine file type with the file command


The file command identifies the type or format of a file. It is a very handy command that can show you how to approach a file when the format is unknown. There are a variety of tests that it uses to determine its type, these tests include a filesystem test, magic number test, and language tests. The first successful test is the one that is printed to the user.

The magic number tests are used to check for files with data in particular fixed formats. To determine the format of a file in this manner, either a magic number identifier exists at the beginning of the file, or a specific identifier can be determined at a specific offset within the file. The file /usr/share/file/magic specifies what magic numbers are to be tested for, what message to print if a particular magic number is found, and additional information to extract from the file.

Here is an example with a directory which contains a variety of file types. The output lists the filename along with its type.

This works equally well when the file names do not have have an extension, which is a very common case with Linux.