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 goWhere
<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
(orfoo.desktop
located in one of the other permitted directories.)From
gtk-launch
documentation:Usable from terminal or Alt + F2 (Alt + F2 stores command in history so it's easily accessible).
The answer should be
But due to a bug (here upstream, closed on 2020-12-09) 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:To break that down
You could put this in a file, say
~/bin/deskopen
with the contentsThen make it executable
And then you could do, e.g.
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. On Arch, it is in kde-cli-tools (HT: @Aron Cederholm).
Since glib 2.67.2 there's a
gio launch
command that can be used like this:For older releases I've got 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
:The script needs to have the executable permission, so I ran this in a terminal:
Then I created the relative
.desktop
entry on~/.local/share/applications/run-desktop.desktop
:Finally, I associated the entry as the default handler in
~/.local/share/applications/mimeapps.list
under the[Default Applications]
section as :Now:
xdg-open
something.desktop works as expected#!/usr/bin/xdg-open
hashbang on top of an executable desktop entry works tooThe Right Way
You should really be using
gtk-launch
if it is available. It is usually part of the package libgtk-3-bin (this may vary by distro).gtk-launch
is used as follows:Please note that
gtk-launch
requires the .desktop file to be installed (i.e. located in/usr/share/applications
or~/.local/share/applications
).So to get around this, we can use a hackish little Bash function that temporarily installs the desired .desktop file before launching it. The "correct" way to install a .desktop file is via
desktop-file-install
but I'm going to ignore that.You can use it like so (and also pass along additional arguments or URIs if you want):
The Manual Alternative
If you want to manually parse and execute a .desktop file, you can do so with the following
awk
command:If you want to treat the
awk
command like an all-in-one script; we can even show an error message and exit with a return code of 1 in the event that an Exec command is not found:The aforementioned commands will:
%f
,%u
,%U
). It is possible to replace these with positional arguments as the specification intends, but doing so would add significant complexity to the problem. See latest Desktop Entry Specification.Note, this AWK script addresses a few edge cases that may or may not be properly addressed by some of the other answers. Specifically, this command removes multiple Exec variables (taking care not to otherwise remove the % symbol), will only execute a single Exec line command, and will behave as expected even if the Exec line command contains one or more equals sign (e.g.
script.py --profile=name
).Just a few other caveats... According to the specification, TryExec is:
With that in mind, it doesn't make sense to execute it's value.
Some other concerns are Path and Terminal. Path consists of the working directory to run the program in. Terminal is a boolean that indicates whether the program is run in a terminal window. These can all be addressed, but there's no sense in reinventing the wheel as there are already implementations of the spec. If you do want to implement Path, keep in mind that
system()
spawns a subprocess, so you can't change working directory by doing something likesystem("cd \047" working_directory "\047"); system(command)
. However you could presumably do something likesystem("cd \047" working_directory "\047 && " command)
. Note \047 are single quotes (so the command doesn't break on paths with spaces).The Python Alternative
I'm stealing a page from Carlo here, who suggested creating a Python script to make use of the gi module. Here's a minimal way to execute the same code from the shell without having to create a file and worry about I/O.
Then execute the launcher function as follows:
Note the use of URIs is optional. Also, no error checking is performed so you'll want to ensure the launcher exists and is readable (before using it) if you want your script to be durable.
You could use dex.
Install dex:
Run the file with it:
seems to work in 13.10 release, if exo-utils is installed (like is the case with Xubuntu).
(Compiled from the various other answers here)
Depending on your system, and the various bugs that may or may not exist on your system, try the following until one of them works:
xdg-open program_name.desktop
exo-open program_name.desktop
[my preferred choice, whenever it works]gtk-launch program_name.desktop
kioclient exec program_name.desktop
dex program_name.desktop
grep
-searches the *.desktop file for theExec=
line containing the command to execute, removes parts of that line we don't want, then executes it. It is well-written and works well for most desktop files. See his answer for a full explanation of it.Note that on Ubuntu systems, your "start menu" desktop launchers are available in
/usr/share/applications/
.As an example, to show which of the above commands do or don't work on my Ubuntu 14.04 system, here are the results of the following calls for me:
xdg-open /usr/share/applications/eclipse_for_cpp.desktop
# Fails due to bug (tries to have me save this .desktop file)exo-open /usr/share/applications/eclipse_for_cpp.desktop
# Worksgtk-launch /usr/share/applications/eclipse_for_cpp.desktop
# Fails with "gtk-launch: no such application"kioclient exec /usr/share/applications/eclipse_for_cpp.desktop
# Worksdex /usr/share/applications/eclipse_for_cpp.desktop
# Fails, &sudo apt install dex
cannot locate package dex$(grep '^Exec' /usr/share/applications/eclipse_for_cpp.desktop | tail -1 | sed 's/^Exec=//' | sed 's/%.//' | sed 's/^"//g' | sed 's/" *$//g') &
# WorksAddendum to Hamish's answer.
Given the deskopen script, you can use a reference to it as the shebang line in a .desktop file, since the comment character is still
#
. That is to say, put this as the first line of the .desktop file:Then flag the .desktop file as executable (e.g. with a
chmod +x whatever.desktop
), and then you canand voilà -- The app will open! (Complete with the icon file I specified, though I have no idea how.)
Now, if you also want deskopen to pass through any command-line parameters, you can instead use this slightly-modified version:
As an aside, I tried using
"#{@:2}"
instead ofshift
ing, but it kept giving me 'bad substitution'...