MIME (Multipurpose Internet Mail Extensions) describes the type of data contained in a file. MIME is widely used to indicate which program should open a data file.
Definition of MIME: see http://www.wikipedia.org/wiki/MIME
A variety of methods are used to determine the MIME type of a file. Internet protocols such as http and e-mail usually give the mime type of data explicitly. When opening a file by clicking it in a file manager, the filename suffix (such as .pdf) and so called "magic numbers" (such as the fixed bytes in an elf header) are commonly used.
Filename suffixes: The system-wide mapping from file suffixes to MIME types is set in /etc/mime.types. Users may set their own suffix to MIME preferences in $HOME/.mime.types. Traditionally file name suffixes are ignored on Unix systems.
For package maintainer, a better way to set up new MIME types is the Shared MIME-info specification from fd.o.
Magic Numbers: Magic numbers are bytes or strings that are set to particular values within files of a given type and which can be used to guess the file type with a reasonable amount of certainty. For instance awk scripts usually start with the line "#!/usr/bin/awk" or "#!/bin/awk", possibly with spaces after the !.
A most useful tool for divining the type of a file based on its contents is the terminal command "file". File "file --mime filename" tries to give the MIME type of filename.
The "file" command uses magic numbers stored in "/usr/share/file/magic". That file is automatically built and so should not be customised as your precious customisations will get lost when the file is rebuilt. The format of the file is described in man magic.
System-wide additions to the magic-number recognition library live in /etc/magic.
Application specific: Applications may have their own MIME-rules. How they implement and record those rules is up to each application. The "application" may be the gnome desktop or a web browser running within gnome, so several rule sets may be checked before /etc/mime.types is referenced.
Default: If the MIME type cannot be determined it defaults to application/octet-stream .
Note: the gnome-file-types-properties that was sometimes referenced is now obsolete and is not present anymore on Debian systems.
The system-wide association between MIME types and applications is generated from the *.desktop files provided by the packages. The .desktop files are stored in the /usr/share/applications/ directory and subdirectories, and the update-desktop-database parses their information and generates the /usr/share/applications/mimeinfo.cache file, that specifies for each MIME type the valid .desktop files (hence applications).
The mimeinfo.cache is basically a raw reverse cache for the .desktop information. There is no way to define priorities in it. To be able to specify default applications, a mimeapps.list file (previously named defaults.list up to debian 5) must be created. It can be system-wide (in /usr/share/applications or a subdirectory) or user-specific (in $HOME/.local/share/applications). A heuristic is used by glib (FIXME: what about KDE?) to use the appropriate mimeapps.list file according to the user environment (Gnome evince vs. KDE Okular for PDF files for instance). The mimeapps.list file follows the same syntax as mimeinfo.cache, i.e:
[Default Applications] application/pdf=evince.desktop image/png=eog.desktop
See https://bugs.freedesktop.org/show_bug.cgi?id=30214 and https://bugzilla.gnome.org/show_bug.cgi?id=630171 for more information.