Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
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 '13 at 13:38
How are magic bytes store in a file in practice? In a text file for example? – Gradient May 16 '13 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. – Oliver Salzburg May 16 '13 at 15:08
Plain text files are mainly recognized from the absence of certain bytes that stand for unprintable characters. – chirlu May 16 '13 at 17:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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