How to Change the Default Application for a File Type in Ubuntu Terminal

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.

  1. Open your terminal window.
  2. Use the mimetype command followed by the name of your file. For example, if you have a file named report.pdf, type:
    mimetype report.pdf
  3. 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.

  1. In the terminal, type the following command, replacing application/pdf with your specific MIME type:
    xdg-mime query default application/pdf
  2. The system will return the name of the desktop entry file (a .desktop file) currently responsible for opening that format, such as evince.desktop or gimp.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.

  1. 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 typing ls /usr/share/applications/.
  2. Once you have identified the correct file (for instance, okular.desktop for the Okular document viewer), run the following command to set it as the new default:
    xdg-mime default okular.desktop application/pdf
  3. The command will execute silently. To verify that the change was successful, run the query command again:
    xdg-mime query default application/pdf
  4. The terminal should now return okular.desktop, confirming that your default application has been successfully updated.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.