3

How can linux know the mimetype of a file? Is there something written inside the file? Does it look at the extension of the file?

1 Answer 1

6

Through the "magic" bytes in a file. You can do this yourself by running the file command:

file --mime filename

To my understanding, the underlying library is libmagic.

4
  • 3
    It does use extensions as well... because things like .txt don't necessarily have good magic bytes to look for. The exact implementation may depend on which Desktop Environment you mean (e.g. KDE v.s. GNOME). However this is an old freedesktop standard, so the behaviour of most DEs should be very similar.
    – sourcejedi
    May 16, 2013 at 13:38
  • How are magic bytes store in a file in practice? In a text file for example?
    – Gradient
    May 16, 2013 at 15:04
  • @Gradient: There are multiple places. Like /usr/share/mime/magic, and /usr/share/file/magic.mgc (binary). You can also add your own signatures in /etc/magic. At least these are valid files on my Debian system. May 16, 2013 at 15:08
  • 2
    Plain text files are mainly recognized from the absence of certain bytes that stand for unprintable characters.
    – chirlu
    May 16, 2013 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .