Home / Alt manpages / xdg-mime(1)

  • xdg-mime(1)
  • User command
  • linux

Set and Check Linux File Handlers with xdg-mime

You will use xdg-mime to identify a file's MIME type, see the desktop entry currently associated with that type, and change the default handler for your desktop user. The examples use xdg-mime 1.1.3 from the xdg-utils 1.1.3-4.1ubuntu3 package.

Allow about ten minutes. You need a graphical desktop session, the xdg-utils package and an application already installed in that desktop's application menu. The query and default commands are ordinary user commands. Do not run them with sudo: the manual limits both operations to a desktop session and does not recommend running them as root.

1. Identify a file type

Start with a file whose type you want to inspect:

$ xdg-mime query filetype /etc/hosts
text/plain

The command prints a MIME type, not a file extension. It may use the file name, its contents or the desktop MIME database. The exact result is therefore a property of the file and the local database, not a value to infer from the suffix.

Checkpoint: confirm the command succeeded before using its result in a script:

$ printf 'exit status: %s\n' "$?"
exit status: 0

The status belongs to the command immediately before printf. A missing path returns status 2, while status 1 indicates a command-line syntax error. Other non-zero statuses mean the requested action failed or a required tool or permission was unavailable.

2. Check the current default application

Pass the MIME type to the default query:

$ xdg-mime query default text/plain
vim.desktop

The output is a desktop file ID, such as vim.desktop. It is not necessarily the executable name. Desktop entries declare which MIME types they support, and the desktop environment decides whether an association request can be accepted.

Do not confuse the two databases involved here. Shared MIME information describes what a type is and how it can be detected. MIME application associations record which application should open it. xdg-mime query filetype reads the first kind of information; query default reads the second.

3. Record the old handler before changing it

A default association is persistent user configuration. Before changing it, save the current desktop entry for the MIME type:

$ MIME_TYPE='text/plain'
$ OLD_HANDLER=$(xdg-mime query default "$MIME_TYPE")
$ printf 'current handler: %s\n' "$OLD_HANDLER"
current handler: vim.desktop

Replace text/plain with the type you actually checked. If the query prints nothing or fails, stop and investigate instead of saving an empty value. The shell variable only lasts for this terminal session, so write the value somewhere safe if you need a rollback after closing the terminal.

4. Set a new default for your user

Warning

The next command changes how your desktop opens every file of the specified type. It does not edit one file; it changes an association. Make sure the application ID is the exact desktop file name and that the application supports the MIME type.

$ xdg-mime default org.gnome.TextEditor.desktop text/plain

Several MIME types can follow one application ID:

$ xdg-mime default org.gnome.TextEditor.desktop text/plain text/markdown

There is normally no useful output on success. Verify the result through the desktop environment rather than assuming that an accepted request became the effective default:

$ xdg-mime query default text/plain
org.gnome.TextEditor.desktop

The application must already be installed in the desktop menu. A desktop policy or an end-user approval step may still prevent the requested handler from becoming effective, so a query is the important checkpoint.

5. Restore the previous association

If the new handler is wrong, restore the value recorded earlier. This is the undo operation for the example:

$ xdg-mime default "$OLD_HANDLER" "$MIME_TYPE"
$ xdg-mime query default "$MIME_TYPE"
vim.desktop

If the original variable is no longer available, use the desktop entry you recorded before the change. If you did not record it, choose the correct existing entry from your desktop settings or application menu rather than guessing a filename.

6. Understand installation of a new MIME type

xdg-mime install is a different operation. It adds descriptions for a new type from an XML file whose document root is mime-info, following the Shared MIME-info Database specification. The file name normally needs an alphabetic vendor prefix followed by a dash, such as acme-report.xml. This prefix reduces name collisions; --novendor disables the check and should be reserved for a deliberate compatibility case.

Install a type for the current user explicitly:

$ xdg-mime install --mode user acme-report.xml

The default mode is user mode for a non-root caller and system mode for root. Prefer the explicit form above so an accidental privilege change does not alter the scope. System mode affects all users and usually requires root, so treat it as an administrative change.

For a custom type, the XML describes the MIME type and matching glob patterns. An icon named for the type may also need installing with xdg-icon-resource. Installing the XML does not automatically choose an application as the default handler. After installation, query a real sample file and set an existing desktop entry only if that is your intended policy.

7. Remove a custom type carefully

uninstall removes descriptions previously added with xdg-mime install:

$ xdg-mime uninstall --mode user acme-report.xml

Removal is a state change and can make files of that type harder for desktop applications to recognise. Keep the original XML so you can reinstall it, and do not remove a system-wide description merely to fix one user's association. Check the type and affected scope first, then remove it only when you control the application that depends on it.

Done means

  • You queried a real file and recorded its MIME type and exit status.
  • You checked the default desktop entry without using elevated privileges.
  • You recorded the old handler before changing an association.
  • You verified the new handler with query default and know how to restore the old one.
  • You understand that MIME installation and default application selection are separate operations.
  • You chose user or system installation mode deliberately and kept the XML needed for recovery.