How to run .desktop files from command line [duplicate]

From what I can gather, .desktop files are shortcuts that allow application's settings to be customized. For instance, I have lots of them in my /usr/share/applications/ folder.

If I open that folder in nautilus, I can run these applications just by double clicking its associated file, e.g. double-clicking firefox.desktop runs Firefox. However, I can't find a way to do the same thing via terminal.

If I do gnome-open foo.desktop it simply opens foo.desktop as a text file. If I make it executable and then run it in bash it simply fails (which is expected, it's clearly not bash script).
EDIT: Doing exec /fullpath/foo.desktop gives me a Permission denied message, even if I change ownership to myself. If I make executable and do the same command, the terminal tab I'm using simply closes (I'm guessing it crashes). Finally, if I do sudo exec /fullpath/foo.desktop, I get an error reporting sudo: exec: command not found.

That's my question, how can I run a foo.desktop file from the terminal?


gtk-launch

With any recent Ubuntu that supports gtk-launch just simply go

gtk-launch <file>

Where <file> is the name of the .desktop file with or without the .desktop part. The name must not include the full path.

The .desktop file must be in /usr/share/applications, /usr/local/share/applications or ~/.local/share/applications.

So gtk-launch foo opens /usr/share/applications/foo.desktop (or foo.desktop located in one of the other permitted directories.)

From gtk-launch documentation:

gtk-launch launches an application using the given name. The application is started with proper startup notification on a default display, unless specified otherwise.

gtk-launch takes at least one argument, the name of the application to launch. The name should match application desktop file name, as residing in /usr/share/application, with or without the '.desktop' suffix.

Usable from terminal or Alt + F2 (Alt + F2 stores command in history so it's easily accessible).


The answer should be

xdg-open program_name.desktop

But due to a bug (here on upstream) this no longer works.


Modern Answer

gtk-launch <app-name> - where <app-name> is the file name of the .desktop file, with or without the .desktop extension.

See another answer on this thread for more details. I got this info from that answer.

Deprecated shell tools answer

Written a long time ago - see the comments below this answer as to why this approach won't work for many desktop files.

The command that is run is contained inside the desktop file, preceded by Exec= so you could extract and run that by:

$(grep '^Exec' filename.desktop | tail -1 | sed 's/^Exec=//' | sed 's/%.//' \
| sed 's/^"//g' | sed 's/" *$//g') &

To break that down

grep  '^Exec' filename.desktop    # - finds the line which starts with Exec
| tail -1                         # - only use the last line, in case there are 
                                  #   multiple
| sed 's/^Exec=//'                # - removes the Exec from the start of the line
| sed 's/%.//'                    # - removes any arguments - %u, %f etc
| sed 's/^"//g' | sed 's/" *$//g' # - removes " around command (if present)
$(...)                            # - means run the result of the command run 
                                  #   here
&                                 # - at the end means run it in the background

You could put this in a file, say ~/bin/deskopen with the contents

#!/bin/sh
$(grep '^Exec' $1 | tail -1 | sed 's/^Exec=//' | sed 's/%.//' \
| sed 's/^"//g' | sed 's/" *$//g') &

Then make it executable

chmod +x ~/bin/deskopen

And then you could do, e.g.

deskopen /usr/share/applications/ubuntu-about.desktop

The arguments (%u, %F etc) are detailed here. None of them are relevant for launching at the command line.


While OP was not asking about KDE, for anyone that is running KDE the following command can be used:

kioclient exec <path-to-desktop-file>

On Fedora, this is included in the kde-runtime rpm.


The bug is still present since 2006. It is in fact depending on how gvfs-open (called by xdg-open) works.

Still, I managed a quick workaround (stealing inspiration from the nautilus source code). It is a bit convoluted, but works flawlessly on Ubuntu 12.10, adding a meaningful icon (no more ?) on the Unity launcher.

First, I wrote a python script using Gio and placed saved it as ~/bin/run-desktop :

#!/usr/bin/python

from gi.repository import Gio
import sys 

def main(myname, desktop, *uris):
    launcher = Gio.DesktopAppInfo.new_from_filename(desktop)
    launcher.launch_uris(uris, None)

if __name__ == "__main__":
    main(*sys.argv)

The script needs to have the executable permission, so I ran this in a terminal:

chmod +x ~/bin/run-desktop

Then I created the relative .desktop entry on ~/.local/share/applications/run-desktop.desktop:

[Desktop Entry]
Version=1.0
Name=run-desktop
Exec=run-desktop %U
MimeType=application/x-desktop
Terminal=false
Type=Application

Finally, I associated the entry as the default handler in ~/.local/share/applications/mimeapps.list under the [Default Applications] section as :

[Default Applications]
....
application/x-desktop=run-desktop.desktop

Now:

  • xdg-open something.desktop works as expected
  • #!/usr/bin/xdg-open hashbang on top of an executable desktop entry works too

It will be useless work when gvfs-open will solve the bug, but in the meantime...