Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 4 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
2  
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
1  
Plain text files are mainly recognized from the absence of certain bytes that stand for unprintable characters. –  chirlu May 16 '13 at 17:31
add comment

Your Answer

 
discard

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.