Running a .desktop file in 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.