Running a .desktop file in the terminal
Solution 1
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.
Solution 2
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).
Solution 3
The answer should be
xdg-open program_name.desktop
But due to a bug (here upstream, closed on 2020-12-09) this no longer works.
Solution 4
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.
Solution 5
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...
Related videos on Youtube
Malabarba
I: am the author of Endless Parentheses; help manage and develop CIDER, the most widely used Clojure IDE; help develop GNU Emacs when I have time; author and manage a number of open source projects, among which: Paradox: Modernized Package Menu for Emacs. sx.el: Full-feature Emacs client for StackOverflow, Super User, and the entire Stack Exchange network. aggressive-indent-mode: Minor mode that keeps your code permanently indented. smart-mode-line: A powerful and beautiful mode-line for Emacs. The Bug-Hunter: Hunt down errors in elisp files. names: A namespace system for elisp.
Updated on September 17, 2022Comments
-
Malabarba over 1 year
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-clickingfirefox.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 opensfoo.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: Doingexec /fullpath/foo.desktop
gives me aPermission 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 dosudo exec /fullpath/foo.desktop
, I get an error reportingsudo: exec: command not found
.That's my question, how can I run a
foo.desktop
file from the terminal?-
Admin over 13 yearsNB: The reason your
exec
failed is because exec replaces your currently running process with the process you specify, so what you did was try to replace your shell with running the desktop as a compiled binary. The reason you couldn'tsudo exec
is because it's a shell builtin and not a binary command. -
Admin over 13 yearsInteresting, I was wondering why it caused the tab to close.
-
Admin almost 13 yearsRelated: Hashbang for Gnome .desktop files
-
Admin almost 13 yearsI see, they end up parsing the .desktop file. Thank you anyway for the link.
-
-
Malabarba over 13 yearsThis gives the best result so far, but it produces undesirable behavior sometimes. It happens whenever the "Exec=" line has an argument like %u or %i. Bash tries to pass that string as a regular argument. For instance, doing
grep '^Exec' firefox.desktop | sed 's/^Exec=//'
opens Firefox with a tab that loads www.%u.com. -
Malabarba over 13 yearsFor the moment I've added a second
sed
to remove any arguments. But I think there might be a more "natural" way to run it. -
Richard Holloway over 13 yearsOn GNOME you can use gnome-open which in turn uses xdg-open which has the bug.
-
Malabarba over 13 yearsDamn =/. Guess that solves it.
-
Hamish Downer over 13 yearsI've updated my answer with the extra sed - I forgot the desktop files could have arguments.
-
enzotib almost 13 yearsNot what I would to hear, anyway thanks for the info
-
enzotib almost 13 yearsThank you, interesting. As "standard" I mean something provided by the DE and conforming to freedesktop.org.
-
Drew over 12 yearsWoW this is still a bug, lots of progress in xdg.
exo-open
is listed as workaround and it opens gedit too. :( -
pabst about 12 yearsI know this is more appropriately a comment to Hamish's answer, but I'm a new user and not allowed to comment. Oh well!
-
daisy about 12 yearsYou should add a "tail -1" to the pipe after "grep" , since "Exec=" can be appear multiple time , and after that , only the last appearance should be executed ..
-
Hamish Downer almost 12 years@warl0ck: thanks for that - I've updated the answer with your suggestion.
-
Flimm over 11 yearsIf you have the reputation, the most appropriate action would be to edit that answer, actually.
-
Flimm over 11 yearsStrike that, anyone can suggest edits, even users who haven't logged in! It's no biggie any way.
-
Bruno Pereira over 11 yearsThis is an answer by itself, its fine.
-
sdaau about 11 yearsThanks for the code - I'm on Lucid, and I simply saved this as
/usr/bin/xdg-openpy
, and gave it achmod +x
- and usedlauncher.launch([],context)
instead of...None,context)
(because of "TypeError: argument 1: Must be sequence, not NoneType"). Nowxdg-openpy app.desktop
works from command line (and all as normal when double-clickingapp.desktop
), and it can remind me if I try to call in terminalxdg-open
and press tab. Cheers! -
MestreLion almost 11 years+1. This is the only answer that does not require manually parsing the
.desktop
file, so it's the most sane (and safe) approach. Also uses moderngi.repository
instead of the deprecatedpygtk
, so great! :) -
MestreLion almost 11 years@RichardHolloway:
gnome-open
does not callxdg-open
, it's the other way around! So the issue lies ingvfs-open
(the successor orgnome-open
) -
MestreLion almost 11 years"no longer works"? It never did!
xdg-open
works by mimetype association, and.desktop
files are associated with text editors, since they are a subclass of text -
MestreLion almost 11 yearsyou can use
"${@:1}"
instead ofshift
, but that requiresbash
instead ofsh
in your#!
shebang. IMHO your original shift approach is simpler and better -
Ingo Leonhardt almost 11 yearsIn fact, this is a question concerning Carlo Pellegrini's answer. I'm a newby, please correct me if there was a better way to place it. The script works really fine, but the icon I get on the Unity launcher is not the icon defined in the .desktop file but the the default icon of the 'Exec'ed command. Any ideas about that?
-
Lucio almost 11 yearsIt doesn't show the icon of the application. Is this an expected behaviour or I should have something wrong in some file?
-
Albert almost 11 yearsThis didn't worked for me. It seems any paths are double quoted. The script by Carlo worked.
-
jmtd over 10 yearsUpvote for
awk
rather than a chain ofgrep
s andsed
s. -
Shirish Herwade over 10 yearsawesome answer... worked for me
-
Sam Watkins almost 10 yearsthis is so stupid (that there is no sensible way to run a Desktop file from the terminal)
-
Sam Watkins almost 10 yearsonly good answer
-
jarno over 9 yearsYes, just tested it in Ubuntu Studio 14.04. BTW. xdg-open and gvfs-open work there, too.
-
basic6 about 9 yearsUpvoted because it works. No need to be running KDE at all, as long as you have this program installed.
-
Admin about 9 yearsThis is the way, works in debian too.
-
Croad Langshan about 9 yearsgtk-launch firefox.desktop ~/.local/share/applications/ launches firefox viewing the directory ~/.local/share/applications/ for me. Seems if you were correct, firefox should not have been passed the directory of the .desktop file as an argument. In fact the directory passed to gtk-launch is not supposed to be used to locate the dir containing the .desktop file (and is not, in fact)
-
Noitidart about 9 yearsThis bug is still there huh?
-
Noitidart about 9 years@IngoLeonhardt did you ever find a fix to that? I'm also trying to get the icon to be that of the
.desktop
file. -
Noitidart about 9 yearsI did as you said I created the shell. and in my desktop file i added the shebang at top. But its not opening with the icon i set on the .desktop file :( I can share a screencast of my situation if you are available :)
-
Ingo Leonhardt about 9 years@noitidart unfortunately not, I've learned to live with it and haven't made any more efforts
-
Noitidart about 9 yearsThanks @IngoLeonhardt for the reply! I'm actually finding lots of documentation on Gio and it has a lot of stuff mentioning
icon
(such asgdk_app_launch_context_set_icon_name
) I think this is very possible! Did you explore theGio
docs? I'm reading code here: github.com/mathitme/nautilus/blob/… -
Ingo Leonhardt about 9 years@noitidart my concern was starting things like specially parametrized xterms with a different icons to be able to distinguish one form another. So I haven't looked at C code. Nevertheless if you find something I would be glad if you let me know
-
Noitidart about 9 yearsWill definitely keep you updated @IngoLeonhardt :) I badly need the custom icon solution :)
-
Noitidart about 9 yearsHey @IngoLeonhardt I figured out the c code to do this and it does launch with the custom icon! This is firefox js-ctypes code. To run it you can follow this youtube vid: youtube.com/watch?v=Rzgf7-I5QPg are you sure the icon of the .desktop file was not used?
-
Noitidart about 9 years@IngoLeonhardt I tested launching .desktop file on Ubuntu 14.04 and it launched and the custom icon of the .desktop took everywhere. But on Mint Cinnamon 17.1 the .desktop file if double clicked or if launched with
Gio
it doesn't take the icon of the .desktop. I'm thinking of looking into usinggdk_app_launch_context_set_icon_name
with Gio launch_uris, maybe it will make it take the icon, any idea? -
Ingo Leonhardt about 9 years@Noitidart not at the moment (and not too much time either). But I've updated my run-desktop to the current version of this answer and still gnome-terminal is launched with it's default icon, not the one form the .desktop (Ubuntu 14.04 too)
-
Ingo Leonhardt about 9 years@Noitidart write the last answer led mo to do some google and I found that. Haven't checked it out, but maybe it helps
-
Noitidart about 9 yearsxseticon looks interesting it must be a custom function which sets
_NET_WM_ICON
-
Noitidart about 9 years@IngoLeonhardt yep XChangeProperty is how they do it i downloaded that file: gist.github.com/Noitidart/64ad2edbda32627b684f
-
Jonathan almost 9 yearsWorks in 15 as well
-
A.B. over 8 yearsBut the
awk
command is nice. Therefore a +1 -
A.B. over 8 yearsNice,
awk
and Serg =) -
A.B. over 8 yearsAnd sometimes, there is a
TryExec
, maybe you should check your 2nd command =) -
Sergiy Kolodyazhnyy over 8 years@A.B. Hehe,
awk
is my weapon of choice when it comes to text processing. Besides, it's syntax is close toC
. Oh, and already addedTryExec
part ^_^ -
A.B. over 8 yearsYou have my +1 =)
-
A.B. over 8 yearsBut what's about
%f
,%u
,%U
or something like this behind the command? -
Sergiy Kolodyazhnyy over 8 years@A.B. that will be treated as part of a field, because
=
is the separator. So, for instance if we haveExec=firefox %u
, then $1 isExec
, separator is=
and $2 isfirefox %u
-
A.B. over 8 yearsAh sorry, my mistake :\
-
Kwaadpepper over 8 yearsIs there a way to have stdout of this ? I have compiled clementine player, which has a bug that occurs only launching through .desktop file (with plasma-shell). And i can't figure out, how to have output log log anywere.
-
Raman over 8 years@Kwaadpepper you could use your .desktop file to launch a shell script, which internally runs your command and redirects output to a file.
-
Alicia about 8 yearsYay! An answer that works!
-
Axel Beckert almost 8 yearsIMHO this is exactly the right answer: One tool, a single call with only the file as parameter. That's what I was looking for, too, to test hand-written
.desktop
files. And it can create.desktop
files, too, yay! :-) -
Stuart Axon over 7 yearsPerfect! If you add a .desktop file that uses dex gist.github.com/stuaxo/4169fc1342c496b7c8f7999188f2f242 to /usr/share/applications/ you will be able to launch desktop files in the filemanager without them opening in gedit by default too.
-
Samuel about 7 yearsThis gave me problems with vlc.desktop, it had an option in the form of
--started-from-file
. I added another pipe to strip that:sed 's/-.*//g'
... I barely know how to use sed, so I don't know if this is without problems. -
king_julien about 7 yearsUsing Debian with xfce. Works great!
-
Victor almost 7 yearsWorks for me on Arch Linux, but maybe it's an Ubuntu-specific bug.
-
Boris Churzin over 6 yearsUpvote, although it doesn't work (or better put - works too much) if you have ultiple
Exec=
lines :/ -
gerardw over 6 yearsWorks for me in Ubuntu 16.04
-
nhed almost 6 yearshackish hack is hacky - need solution that causes the desktop to run it
-
wisbucky almost 6 yearsDoesn't work for me in Ubuntu 16.04. Just opens the
.desktop
file in gedit. -
MountainX over 5 yearsThis does NOT work for me in Arch Linux at this time.
xdg-open program_name.desktop
opens the desktop file in a text edtor. -
MountainX over 5 yearsThis DOES work for me in Arch Linux also. None of the other answers were satisfactory, but this one is good. :-) It works even though I am running KDE.
-
MountainX over 5 yearsI do not find kioclient in any package of KDE for Arch Linux at this time. However, gtk-launch works under KDE for me.
-
Joseph Garvin over 5 yearsNo version of this works for me on Ubuntu 18.10. Everytime it complains the application doesn't exist, whether I'm in the folder with the desktop file or not, whether I include the .desktop extension or not, and whether I separately name the directory or not.
-
Joseph Garvin over 5 yearsWhat is a URI in this context? I have a desktop file in an arbitrary folder. How the heck do I just run gtk-launch on it without having to wrap it in some other script? This is maddening.
-
doug over 5 years@ Joseph, I didn't add that part about providing a path, some one else did and probably is wrong. The .desktop needed to be in an applications folder as noted. (/usr/share/applications, /usr/local/share/applications or ~/.local/share/applications
-
Tyler Collier over 5 yearsWorks for me in Ubuntu 14.04
-
MarSoft about 5 yearsThis
awk
solution will not properly work if the command has double-escaped whitespaces or backslashes. It breaks on this:Exec=env WINEPREFIX="/path/to/.wine" wine c:\\\\windows\\\\command\\\\start.exe /Unix /path/to/.wine/dosdevices/c:/users/Public/Рабочий\\ стол/appname.lnk
Anddex
solution works fine. -
tatsu about 5 yearssame ubuntu 18.10 still opens gedit
-
tatsu about 5 yearswell that's utterly pointless. the only real point to would be to launch user-made .desktop launchers. if your app is in
/usr/local/share/applications
then you can just type the name of the app in the terminal. -
tatsu about 5 years@JosephGarvin Okay I figured it out, it's gtk-launch MYCUSTOMAPP.desktop without the path. works on ubuntu 18.10
-
itmuckel about 5 yearsWorks for me in 18.04 using Xubuntu. According to the help output of xdg-open and xdg-mime it shouldn't.
-
Bob C over 4 yearsThis answer fails when the exec string ended in a double quote mark. It removed the last one but left the first one. For example:
Exec=su-to-root -c "/usr/local/bin/ddm-mx -i nvidia"
resulted in:su-to-root -c "/usr/local/bin/ddm-mx -i nvidia
. -
jarno over 4 yearsThe files could be even in any data dir specified in specifications.freedesktop.org/basedir-spec/…
-
kokbira over 4 yearsIt solves that issue for Fedora 30 too
-
ricab about 4 yearsThat's it! Too much scrolling for the right answer...
-
FallenWarrior about 4 yearsThank you for this. I was looking for a way to bypass this issue, while still making use of the .desktop files (for things like
%U
handling), andgtk-launch
worked perfectly. I did download and install the patched version of Nautilus from the MR linked there, but it's great to have a solution that works without downgrading a core system (in terms of GNOME) application. -
Penghe Geng almost 4 yearsWorks on 20.04.
-
knezi over 3 years@BobC yep, just change the two last seds to:
sed 's/^"\(.*\)"$/\1/g'
-
geekley over 3 yearsThis doesn't seem to work for files in
~/.local/share/applications/
. -
geekley over 3 years
kioclient5 exec ~/.local/share/applications/myprogram.desktop
worked for me on Kubuntu 20.04 -
geekley over 3 yearsAh!
kioclient5 exec ~/.local/share/applications/myprogram.desktop
worked for me on Kubuntu 20.04. See answer below if you're on KDE. -
chenchuk over 3 yearsThe only one that worked for me so far. Thanks.
-
shadowtalker over 3 yearsCan
exo-open
be installed as a standalone package on Ubuntu without pulling in all of XFCE? I ask because in Opensuseexo-tools
is a standalone package, with only minimal dependencies on XFCE (I think just one core library). -
Gabriel Staples about 3 yearsI like your "Deprecated shell tools answer", which, albeit imperfect for all cases, works for most desktop files. So, I've upvoted your answer and put that script into my own summary answer too.
-
Zanna about 3 yearsDoes this actually work? I don't think
.desktop
files have interpreter directives, so, like, won't the shell just unsuccessfully try to execute the file? -
thiagowfx over 2 years
$ dex chromium.desktop
yields:File does not exist: /home/$USER/chromium.desktop
. This solution isn't very portable, you're otherwise forced to use$ dex /usr/share/applications/chromium.desktop
.gtk-launch
works without the full path. -
RatajS over 2 years@geekley It worked in KDE Neon 5.22.4 (Ubuntu 20.04) as well.
-
Alexei Martianov over 2 yearsFor those who found that answer by googling and wanted to start desktop file, for
gtk-launch
the file has to be located in certain folders, see unix.stackexchange.com/questions/393079/… -
Ivan Marjanovic over 2 yearsNo, this is not working
-
Aron Cederholm about 2 years@MountainX, it is located in package kde-cli-tools