Linux MIME Types and Default Applications Explained
The Multipurpose Internet Mail Extensions (MIME) type system is the foundational mechanism Linux operating systems use to identify file formats and assign default applications to open them. Unlike systems that rely strictly on file extensions, Linux utilizes standardized MIME types alongside specifications from FreeDesktop.org to inspect file contents, map them to recognized media formats, and route user actions to the appropriate software. This article explains how the MIME type database works, how default associations are registered and prioritized, and how desktop environments utilize these standards to open files seamlessly.
What Are MIME Types in Linux?
A MIME type is a standardized two-part identifier consisting of a
type and a subtype separated by a slash (for example,
text/plain, image/png, or
application/pdf). Originally created for email attachments,
modern Linux systems use MIME types as the universal language for
categorizing data streams and local files.
How Linux Identifies File Types
To determine what a file actually is, Linux relies on the
shared-mime-info database maintained under FreeDesktop.org
specifications. The system evaluates files using two primary
methods:
- Glob Matching (File Extensions): The system checks
the file name against a database of known extensions (e.g., matching
.htmltotext/html). - Magic Number Inspection: If an extension is missing, ambiguous, or misleading, the system inspects the file's raw initial bytes—known as "magic numbers"—to detect its true signature (e.g., detecting an ELF binary or a JPEG header).
Once this analysis is complete, the operating system assigns the correct MIME type string to the file.
Mapping
MIME Types to Applications via .desktop Files
Applications installed on a Linux system register their capabilities
through desktop entry files, typically stored in
/usr/share/applications/ or
~/.local/share/applications/ with a .desktop
extension.
Inside these files, developers include a MimeType field
listing every format the program can handle:
[Desktop Entry]
Name=Image Viewer
Exec=image-viewer %U
MimeType=image/png;image/jpeg;image/gif;When new software is installed, the system updates its MIME cache
databases, mapping each MIME type to a list of eligible
.desktop files capable of handling that format.
Determining
the Default Application with mimeapps.list
When multiple installed programs can handle the same MIME type (such
as multiple web browsers or text editors), the system relies on
configuration files named mimeapps.list to resolve which
program is the default.
These files are read in a specific order of precedence:
- User Configuration:
~/.config/mimeapps.list(highest priority) - Desktop-Specific User Defaults:
~/.config/<desktop>-mimeapps.list - System-Wide Overrides:
/etc/xdg/mimeapps.list - System-Wide Package Defaults:
/usr/share/applications/mimeapps.list(lowest priority)
Inside mimeapps.list, associations are defined under
explicit sections:
[Default Applications]
text/html=firefox.desktop
image/png=org.gnome.eog.desktop
application/pdf=org.gnome.Evince.desktop
[Added Associations]
text/plain=code.desktop;gedit.desktop;The [Default Applications] section explicitly binds a
MIME type to a specific .desktop launcher.
The Execution Workflow
When a user double-clicks a file in a graphical file manager or runs
a command like xdg-open document.pdf, the system performs
the following sequence:
- Sniff: Identifies the file format and returns
application/pdf. - Lookup: Consults the user and system
mimeapps.listfiles to find the default application assigned toapplication/pdf. - Locate: Finds the designated
.desktopfile (e.g.,org.gnome.Evince.desktop). - Execute: Reads the
Execcommand line from the.desktopfile and launches the application with the target file path as an argument.
By decoupling file extensions from executable programs and relying on a centralized MIME database, Linux provides a reliable, desktop-agnostic system for managing file associations across diverse environments.