The xdg-open command in Linux: opening files with the default application

Introduction

In Linux-based operating systems, modern desktop environments such as GNOME, KDE, or XFCE provide a convenient way to open files simply by double-clicking them. Behind that action lies a mechanism that determines which application is responsible for handling each file type. From the terminal, the same behavior can be reproduced thanks to the xdg-open command, which acts as a bridge between the command line and the desktop’s MIME association system. Understanding how it works allows automating tasks, creating more flexible scripts, and avoiding having to remember the exact name of each default program.

What is xdg-open?

xdg-open belongs to the set of XDG utilities (X Desktop Group), now known as the Portland Project, which aims to standardize interaction between applications and the desktop environment on Unix-like systems. Its sole responsibility is to receive a path or a URL and delegate opening to the associated file handler according to the detected MIME type. It does not contain its own logic for displaying images, playing videos, or editing documents; it simply queries the database of default applications and launches the corresponding program. In this way, xdg-open becomes a neutral tool that works indistinguishably in GNOME, KDE, XFCE, LXQt, or any other desktop that follows the XDG standard.

How xdg-open works

When xdg-open is executed followed by an argument, the program first checks whether the argument is a URL (http, https, ftp, file) or a local path. If it is a URL, it uses the registered scheme handler in the desktop. If it is a path, it obtains the file’s extension or, even better, reads its content to determine the MIME type using the shared-mime-info library. Once the MIME type is known, it consults the default applications configuration file (usually located at ~/.local/share/applications/mimeapps.list or /usr/share/applications/defaults.list) to find the associated .desktop file. Finally, it executes the command specified in that .desktop file, passing the file or URL as a parameter.

Basic usage

The simplest use of xdg-open consists of writing the command followed by the name of the file you wish to open. For example, xdg-open documento.pdf will launch the default PDF viewer, whether Evince, Okular, or any other that the user has configured. If a URL is passed, such as xdg-open https://example.com, the default web browser will open. The command does not require special privileges; it runs with the privileges of the user executing it. Moreover, it accepts multiple arguments, opening each in its own instance of the corresponding application, which is useful for processing batches of files from a script.

Practical examples

  • xdg-open imagen.png → opens the image with the default image viewer (for example, Eye of GNOME or Gwenview).
  • xdg-open presentación.odp → launches the office suite configured for OpenDocument files, such as LibreOffice Impress.
  • xdg-open música.mp3 → plays the track with the default audio player (Rhythmbox, Clementine, etc.).
  • xdg-open https://wiki.archlinux.org → opens the page in the default web browser.
  • xdg-open archivo.tar.xz → if the user has associated a file manager, it will show the contents inside the manager; otherwise, it may launch an extraction tool according to the MIME configuration.

Customizing associations

Although distributions come with reasonable default associations, users often prefer other applications for certain file types. Associations can be modified in two main ways: via the desktop’s graphical interface (for example, right-clicking a file, selecting «Properties», and changing the «Open with» option) or by directly editing the mimeapps.list file located in the ~/.local/share/applications directory. In that file, under the [Added Associations] and [Default Applications] sections, you can add or overwrite lines of the form mimeType=name.desktop. After saving the changes, xdg-open will immediately reflect the new preference without needing to restart the session.

Limitations and considerations

It is important to remember that xdg-open depends entirely on the desktop environment and the services provided by the XDG standard. On servers without a graphical interface or in pure terminal sessions, the command may fail or return an error indicating that no suitable handler was found. Likewise, if multiple applications are registered for the same MIME type, the system will choose the one marked as default; however, some environments allow defining per-application preferences via environment variables such as XDG_CURRENT_DESKTOP. Lastly, although xdg-open handles local files and common URLs well, it does not support less common schemes unless a handler has been previously registered.

Conclusion

In summary, xdg-open is a key piece for integrating the command line with the desktop experience on Linux. Its simplicity hides a powerful ability to respect user preferences and open any resource with the appropriate application, without needing to know the specific program names. Whether for automating backups, opening documentation from a script, or simply launching a file while working in the terminal, mastering xdg-open improves productivity and cohesion between the graphical environment and the console. Incorporating it into daily workflows is a recommended practice for both novice users and experienced system administrators.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .