Explain please what is a magic file in unix

23,198

In Unix a file is just a sequence of bytes, however some files have more structure. The file command can look at the sequence of bytes and tell you things.for example if the first 4bytes are \177 E L F then it will tell you it is an elf file. It will then look at more bytes and tell you if it is a core file, an executable or an object file. If the bytes 6 thru 9 are JFIF then it is a picture file and again the program can read more and tell you about it.

A lot of this is driven by data tables rather than being compiled into the program. These data tables are what is in the magic files.

So the reason why you have magic files is to allow you to say file * and get the program to tell you what you have in the current directory.

Share:
23,198

Related videos on Youtube

Kusalananda
Author by

Kusalananda

Updated on September 18, 2022

Comments

  • Kusalananda
    Kusalananda over 1 year

    We can create magic files with file -C -m filename. Also there is the /usr/share/file/magic folder with a list of magic files on my OS X machine.

    But what is this? Would somebody explain, why should we have magic files? How should we create a magic file?

    I read the man pages (man file and man magic), but it is still not clear.

    • Admin
      Admin over 6 years
      Look at the file(1) man page and it may become clear.
    • Admin
      Admin over 6 years
      What parts of the manpages did you not understand?