Introduction
In Linux-based operating systems, modern desktop environments such as GNOME, KDE or XFCE offer a comfortable way to open files simply by double clicking on them. Behind that action is a mechanism that determines which application is in charge of handling each type of file. From the terminal, the same behavior can be played thanks to the xdg-open command, which acts as a bridge between the command line and the MIME association system on the desktop. Knowing its operation allows you to automate tasks, create more flexible scripts and avoid having to remember the exact name of each default program.
What is xdg-open?
xdg-open belongs to the XDG (X Desktop Group) utility set, now known as Portland Project, which seeks to standardize the interaction between applications and the desktop environment in Unix-like systems. Your only responsibility is to receive a route or URL and delegate the opening to the associated file manager according to the MIME type detected. It does not contain its own logic for showing images, playing videos or editing documents; it simply consults the database of default applications and launches the corresponding program. In this way, xdg-open becomes a neutral tool that works indistinctly in GNOME, KDE, XFCE, LXQt or any other desktop that follows the XDG standard.
How xdg-open works
When xdg-open is run followed by an argument, the program first checks whether the argument is a URL (http, https, ftp, file) or a local route. If it is a URL, use the schematic handler on the desktop. If it is a route, get the file extension or, better yet, read its content to determine the MIME type by using the share-mime-info library. Once the MIME type is known, check the default application configuration file (usually located in ~/ .local / share / applications / mimeapps.list or / usr / share / applications / defaults.list) to find the .desktop file associated. Finally, run the command specified in that .desktop file, passing the file or URL as a parameter.
Basic use
The simplest use of xdg-open is to write the command followed by the name of the file you want to open. For example, xdg-open documento.pdf will launch the default PDF viewer, be it Evince, Okular or any other user has set up. If you pass a URL, like xdg-open https: / / example.com, the default web browser will open. The command does not require special permissions; it works with the privileges of the user who runs it. In addition, it accepts multiple arguments, opening each in its own instance of the corresponding application, which is useful for processing file lots from a script.
Practical examples
- xdg-open imagen.png → opens the image with the default photo viewer (e.g. Eye of GNOME or Gwenview).
- xdg-open presentation.odp → launches the office suite configured for OpenDocument files, such as LibreOffice Print.
- xdg-open musica.mp3 → reproduces 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 file .tar.xz → if the user has a file manager associated, will display the content within the manager; otherwise, you can launch an extraction tool according to the MIME configuration.
Customize partnerships
Although distributions come with reasonable default associations, users often prefer other applications for certain file types. The partnerships can be modified in two main ways: by using the graphical interface of the desktop (for example, by right clicking on a file, selecting «Properties» and changing the option «Open with») or directly editing the mimeapps.list file in the directory ~/ .local / share / applications. In this file, under the [Added Associations] and [Default Applications] sections, the mimeType = nombre.desktop. lines can be added or overwritten. After saving the changes, xdg-open will immediately reflect the new preference without reopening 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 can fail or return an error indicating that no suitable handle was found. Also, if there are multiple registered applications for the same MIME type, the system will choose the one that is marked as default; however, some environments allow to define preferences by application using environment variables such as XDG _ CURRENT _ DESKTOP. Finally, although xdg-open handles local files and common URLs well, it does not support less common schemes without a previously registered handler.
Conclusion
In short, xdg-open is a key piece to integrate the command line with the Linux desktop experience. Its simplicity conceals a powerful ability to respect the user's preferences and open any resource with the right application, without knowing the specific names of the programs. Whether to automate backups, open documentation from a script or simply release a file while working on the terminal, master xdg-open improves productivity and cohesion between the graphic environment and the console. Incorporating it into daily workflows is a recommended practice for both new users and experienced system managers.


