Set and Check Linux File Handlers with xdg-mime
Double-click the wrong file type once and xdg-mime is how you find out which application actually claimed it, and how you hand it back. This covers identifying a file's MIME type, checking the desktop entry associated with it, and changing the default handler for your user. Examples use xdg-mime 1.1.3 from the xdg-utils 1.1.3-4.1ubuntu3 package.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a graphical desktop session, the xdg-utils package, and an application already installed in that desktop's menu.
Warning
Do not run the query or default commands with sudo. The manual limits both 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 result is a property of the file and the local database, not something you can 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, and status 1 means a command-line syntax error. Any other non-zero status means 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, 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.
Two databases are involved here and it is easy to conflate them. Shared MIME information describes what a type is and how it is detected; xdg-mime query filetype reads that. MIME application associations record which application should open it; query default reads that instead.
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 rather than saving an empty value. The shell variable only lasts for this terminal session, so write it down somewhere safe if you might need to roll back 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, not just one file. Make sure the application ID is the exact desktop file name and that the application actually 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 through the desktop environment rather than assuming 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 can still block the requested handler, so treat the query as the real checkpoint, not the command's exit status.
5. Restore the previous association
If the new handler is wrong, restore the value you recorded earlier:
$ xdg-mime default "$OLD_HANDLER" "$MIME_TYPE"
$ xdg-mime query default "$MIME_TYPE"
vim.desktop
Recovery
If the shell variable is gone, use the desktop entry you wrote down before the change. If you did not record it, pick 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 separate 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 cuts down name collisions, and --novendor disables the check only for a deliberate compatibility case.
$ xdg-mime install --mode user acme-report.xml
- Prefer the explicit
--mode. The default is user mode for a non-root caller and system mode for root, so an accidental privilege change can silently alter the scope. - Installing the XML does not set a default handler. The XML only describes the MIME type and matching glob patterns. Query a real sample file afterwards and set an existing desktop entry only if that is genuinely your intended policy.
- An icon may need installing separately with
xdg-icon-resourceif the type needs one.
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: it 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 just to fix one user's association. Check the type and the scope it affects first, and only remove it 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 elevated privileges.
- You recorded the old handler before changing any association.
- You verified the new handler with
query defaultand know how to restore the old one. - You keep MIME installation and default selection separate in your head, since they are separate operations.
- You chose user or system mode deliberately and kept the XML needed for recovery.