1. Overview

Sometimes, we might need to run a .desktop or Desktop entry file from the command line without relying on a graphical user interface (GUI). This method is particularly useful in different scenarios, such as in automation, scripting, or working in remote environments.

In this tutorial, we’ll check out methods to run a .desktop file from the command line using the gtk-launch, dex, exo-open, and awk commands, as well as the Bash command substitution.

Notably, our examples involve running the gnome-system-monitor.desktop test file in an Ubuntu 22.04 GNOME desktop environment.

2. Using the gtk-launch Command

gtk-launch is used for launching GTK-based applications from the command line in a graphical environment. In particular, we can also utilize it to run a .desktop file from the command line interface (CLI).

For instance, we’ll use the gtk-launch command to run a .desktop file:

$ gtk-launch gnome-system-monitor.desktop

The above script runs the gnome-system-monitor.desktop file placed in our Desktop directory:

launching .desktop file with gtk-launch command

Consequently, the gtk-launch command runs the gnome-system-monitor.desktop file and launches the System Monitor application from the terminal.

3. Using the dex Command

dex, short for desktop entry, is another powerful utility that generates and executes .desktop files of the application type. Specifically, it provides a consistent experience for launching applications on Linux-based systems.

However, in this case, we’ll use the dex command to run a .desktop file from the terminal.

First, we install the dex utility on our system via apt:

$ sudo apt-get install dex

After a successful installation, we use dex to run the gnome-system-monitor.desktop file from the command line:

$ dex gnome-system-monitor.desktop

Let’s check the result:

launching .desktop file with dex command

Thus, we can see the System Monitor application running on our desktop.

4. Using the exo-open Command

On Linux, the exo-open command enables us to open a given application from a particular category or launch URLs with the default URL handler.

Moreover, we can also use it to run a .desktop file from the command line.

To begin with, let’s install exo-utils, the package that contains exo-open:

$ sudo apt-get install exo-utils

Now, we can run the exo-open command in our terminal:

$ exo-open gnome-system-monitor.desktop

Here, we execute exo-open to run the gnome-system-monitor.desktop file:

launching .desktop file using exo-open command

Consequently, the gnome-system-monitor.desktop file launches the System Monitor application on our system.

5. Using awk

We can also combine the awk text processing tool and the Exec Desktop entry file command field for advanced execution of code according to the specified pattern in the text data.

To elaborate, we employ awk to scan for the Exec command within a .desktop file and run that from the command line.

Now, let’s look at an example:

$ $(awk -F= '/^Exec/ {print $2; exit}' gnome-system-monitor.desktop)

Here, we passed gnome-system-monitor.desktop file for execution:

launching .desktop file with awk and exec command

In the above script, the awk command operates on the contents of the gnome-system-monitor.desktop file. In particular, it uses the -F= equals sign field separator to search for the line starting with Exec.

Once awk finds such a line, it prints the second field $2, which contains the command to execute the associated application. Notably, the exit statement tells awk to process only the first matching line.

Furthermore, the code enclosed in the $() substitution executes and replaces its output within the top-level command.

Finally, we can see the System Monitor application running on our system.

6. Using Bash Command Substitution

In Linux, Bash command substitution is a feature that enables the output of simple or complex commands to replace the commands themselves within a statement.

Additionally, we can use it with other commands such as grep and sed to run a .desktop file from the CLI.

Now, we can consider an example in detail:

$ bash -c "$(grep '^Exec' gnome-system-monitor.desktop | sed 's/^Exec=//' | sed 's/%.//')"

Let’s break down this code:

  • grep extracts lines from the gnome-system-monitor.desktop file that starts with Exec
  • the first pipe (|) operator redirects the output of grep to sed
  • the first sed removes the Exec prefix from the extracted line using the ‘s/^Exec=//’ pattern and leaves only the command part
  • the second pipe (|) redirects the output to another sed command
  • the second sed removes any trailing % from the command part using the ‘s/%.//’ pattern
  • bash with the -c option executes the command substitution within a new Bash shell

Naturally, we expect the result to be similar to the ones in the previous sections:

launching .desktop file using bash substitution

So, we can see that the gnome-system-monitor.desktop file runs the System Monitor application from the command line.

7. Conclusion

In this article, we explored several methods to run a .desktop file from the command line.

Particularly, we can use the gtk-launch and exo-open commands for running the .desktop file with ease. Meanwhile, dex also offers efficient execution of a Desktop entry file.

In addition, we can also use awk to process the Exec command within the .desktop file or even Bash command substitution. However, these two approaches are comparatively more complex than others.

Comments are closed on this article!