LinuxCommandLibrary

xdg-mime

Query or change file type associations

TLDR

Display the MIME type of a file

$ xdg-mime query filetype [path/to/file]
copy

Display the default application for opening PNGs
$ xdg-mime query default [image/png]
copy

Display the default application for opening a specific file
$ xdg-mime query default $(xdg-mime query filetype [path/to/file])
copy

Set imv as the default application for opening PNG and JPEG images
$ xdg-mime default [imv.desktop] [image/png] [image/jpeg]
copy

SYNOPSIS

xdg-mime query {filetype | default} [FILENAME | MIMETYPE]
xdg-mime install [--mode mode] MIMETYPE_FILE
xdg-mime uninstall [--mode mode] MIMETYPE_FILE
xdg-mime --help
xdg-mime --version

PARAMETERS

query filetype FILENAME
    Determines and outputs the MIME type of the specified FILENAME. This uses file content and name to infer the type based on the system's MIME database.

query default MIMETYPE
    Outputs the desktop entry (.desktop file) of the default application registered for the specified MIMETYPE. For example, application/pdf.

install [--mode mode] MIMETYPE_FILE
    Installs a MIME type specification contained in the MIMETYPE_FILE. The file must be an XML file adhering to the Shared MIME Info Specification.

uninstall [--mode mode] MIMETYPE_FILE
    Uninstalls a previously installed MIME type specification specified by MIMETYPE_FILE.

--mode system
    Used with install or uninstall to perform the operation system-wide (for all users). This typically requires root privileges.

--mode user
    Used with install or uninstall to perform the operation for the current user only. This is the default mode if --mode is not specified.

--help
    Displays a brief help message and usage instructions for the command.

--version
    Outputs the version information of the xdg-utils package that includes xdg-mime.

DESCRIPTION

xdg-mime is a command-line utility from the xdg-utils package, designed to manage MIME type associations and default applications on a Linux desktop environment. It provides a standardized way to query the MIME type of a file, determine the default application for a given MIME type, and install or uninstall new MIME type definitions.

Adhering to the Freedesktop.org Shared MIME Info Specification, xdg-mime helps ensure cross-desktop compatibility for how various file types are handled. It abstracts away the differences between various desktop environments like GNOME, KDE, and XFCE, providing a consistent interface for users and applications to interact with the system's MIME database.

CAVEATS

xdg-mime requires the xdg-utils package to be installed and a Freedesktop.org compliant desktop environment. System-wide install and uninstall operations typically require superuser privileges. Changes to MIME types might not take effect immediately and often require a desktop session restart or manual execution of update-mime-database.

MIME DATABASE LOCATIONS

MIME type definitions are typically stored in XML files under directories like /usr/share/mime (system-wide) and ~/.local/share/mime (user-specific). After installing or uninstalling MIME types, the MIME database often needs to be updated using update-mime-database for changes to take full effect in the system.

USAGE EXAMPLE: QUERYING A FILE TYPE

To find out the MIME type of a file named report.odt:
xdg-mime query filetype report.odt
This command would typically output something like application/vnd.oasis.opendocument.text.

USAGE EXAMPLE: QUERYING DEFAULT APPLICATION

To find out the default application set for PDF files (application/pdf):
xdg-mime query default application/pdf
This command might output a desktop file name like evince.desktop or okularApplication_pdf.desktop, indicating the associated viewer.

HISTORY

xdg-mime is a core component of the xdg-utils suite, developed under the Freedesktop.org project. This project commenced in the early 2000s with the primary goal of standardizing desktop environment interactions across Linux and Unix-like operating systems. The xdg-utils, including xdg-mime, were designed to provide a set of command-line tools that abstract away desktop-specific differences, ensuring applications and scripts behave consistently regardless of the user's chosen desktop environment (e.g., GNOME, KDE, XFCE). Its development has been driven by the need for robust and cross-desktop MIME type management, relying on standards like the Shared MIME Info Specification.

SEE ALSO

Copied to clipboard