If you primarily manage your Ubuntu Linux desktop from the terminal, you may occasionally find that a specific file type opens in the wrong application. For example, your system might stubbornly open PDF files in an image viewer instead of your preferred document reader, or launch Python scripts in a text editor rather than executing them. While you can change these associations through the graphical user interface, knowing how to manage them via the command line is much faster and essential for remote administration.
In Ubuntu and other Debian-based distributions, file associations are managed by a utility called xdg-mime. This tool allows you to query which application is currently set to open a specific file type, and quickly assign a new default application.
Finding the Correct MIME Type
Before you can change the default application, you must first identify the exact MIME type (the standard identifier for file formats) of the file you are working with.
- Open your terminal window.
- Use the
mimetypecommand followed by the name of your file. For example, if you have a file named report.pdf, type:mimetype report.pdf - The terminal will output something like:
report.pdf: application/pdf. The text after the colon (application/pdf) is the exact MIME type you need to target.
Checking the Current Default Application
Now that you know the MIME type, you can check which application is currently assigned to handle it.
- In the terminal, type the following command, replacing application/pdf with your specific MIME type:
xdg-mime query default application/pdf - The system will return the name of the desktop entry file (a .desktop file) currently responsible for opening that format, such as
evince.desktoporgimp.desktop.
Setting a New Default Application
To change the default behaviour, you must tell xdg-mime to associate your desired application’s .desktop file with the target MIME type.
- First, ensure you know the exact name of the .desktop file for the application you want to use. These files are typically stored in
/usr/share/applications/. You can list them by typingls /usr/share/applications/. - Once you have identified the correct file (for instance,
okular.desktopfor the Okular document viewer), run the following command to set it as the new default:xdg-mime default okular.desktop application/pdf - The command will execute silently. To verify that the change was successful, run the query command again:
xdg-mime query default application/pdf - The terminal should now return
okular.desktop, confirming that your default application has been successfully updated.