0

Reading the documentation about the Linux file command. I have found this in the magic test description:

Any file with some invariant identifier at a small fixed offset into the file
can usually be described in this way. The information identifying these files
is read from /etc/magic and the compiled magic file /usr/share/misc/magic.mgc,
or the files in the directory /usr/share/misc/magic if the compiled file
does not exist. In addition, if $HOME/.magic.mgc or $HOME/.magic exists,
it will be used in preference to the system magic files.

I can imagine that the magic file is a collection of "MagicNumber -> Kind of file" relation. That the "file" program runs to find the kind of file, taking the header of the file. But I no understand why:

  • Why this file should be compiled?
  • What is the way than you can create a magic compile file to use with the command?
  • What is the usage of command options related to this kind of test and file? Ex: -C or -m.
1
  • Why this file should be compiled?

I suspect it's for performance reasons. The magic database is not small. File would need to parse every human-readable magic source file, construct the structures it uses to detect file formats, compute the strength of every pattern and sort everything by that. This process could be unbearably slow, especially decades ago (file has been around since the '70s).

I guess they could let file build the database when it's executed and cache the results, but given the use case it seems completely unnecessary, complicated and would have a number of other issues.

  • What is the way than you can create a magic compile file to use with the command?
file -m MAGIC_SOURCE -C

This creates a compiled magic file with the same base name as MAGIC_SOURCE and extension .mgc in your current working directory.

You can use that on a file or directory. Avoid the trailing slash for directories.

For instance, file -m ~/.magic -C compiles all the ~/.magic/* sources into a single .magic.mgc compiled file.

  • What is the usage of command options related to this kind of test and file? Ex: -C or -m.

-C tells file to compile some source files.

-m tells file which magic file (source or compiled) to use. You can use it without -C too, for instance to use any file, including source ones, to detect a file type.

See man file for more information on the command and man magic for more information on the magic source file syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.