20

I was reading about the file command and I came across something I don't quite understand:

file is designed to determine the kind of file being queried.... file accomplishes this by performing three sets of tests on the file in question:

  • filesystem tests,
  • magic tests,
  • language tests

What are magic tests?

Gilles 'SO- stop being evil'
865k204 gold badges1.8k silver badges2.3k bronze badges
asked Jun 21, 2014 at 10:24

2 Answers 2

29

"magic" here refers to "magic numbers": a special value that's in a known place in a file that identifies its type. The file command has a database of these numbers and what type they correspond to. The library that goes with that database is called libmagic, and you can access that from your own programs.

They aren't necessarily "numbers" as we might think of them. For example, a PNG image file always starts with "\x89PNG\r\n\x1a\n", a Java class begins with the four bytes (in hexadecimal) CA FE BA BE, and an HTML file has "< html" somewhere near the start. It's just some small sequence of data that's known to be in a file of that type, usually very close to the start.

When people are defining file formats they often include one of these in it either deliberately or just as part of making the format fit together. file can use them afterwards. It also has other ways of actually looking at the contents of the file to guess what it is ("language tests").

Anthon
81.4k42 gold badges174 silver badges228 bronze badges
answered Jun 21, 2014 at 10:36
1
  • 6
    Note that originally, the "magic numbers" were, specifically, the first two bytes of an executable file, used by the kernel to load it in the appropriate way. #! is actually an example of this, because the kernel itself, on seeing those bytes, is supposed to invoke the command that follows. Commented Jun 22, 2014 at 0:05
16

That refers to the "magic bytes" which many file formats have at the beginning of a file which show what kind of file this is.

E.g. if a file starts with #! then it is considered a script.

answered Jun 21, 2014 at 10:30
2
  • Who writes these "magic bytes" in the file? Commented Dec 26, 2024 at 19:34
  • @AntoniosSarikas The application which creates the file. Or the user, manually, e.g. when creating a shell script with an editor. Commented Dec 27, 2024 at 0:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.