Running a .desktop file in the terminal

279,972

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...

Share:
279,972

Related videos on Youtube

Malabarba
Author by

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, 2022

Comments

  • Malabarba
    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-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?

    • Admin
      Admin over 13 years
      NB: 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't sudo exec is because it's a shell builtin and not a binary command.
    • Admin
      Admin over 13 years
      Interesting, I was wondering why it caused the tab to close.
    • Admin
      Admin almost 13 years
    • Admin
      Admin almost 13 years
      I see, they end up parsing the .desktop file. Thank you anyway for the link.
  • Malabarba
    Malabarba over 13 years
    This 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
    Malabarba over 13 years
    For 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
    Richard Holloway over 13 years
    On GNOME you can use gnome-open which in turn uses xdg-open which has the bug.
  • Malabarba
    Malabarba over 13 years
    Damn =/. Guess that solves it.
  • Hamish Downer
    Hamish Downer over 13 years
    I've updated my answer with the extra sed - I forgot the desktop files could have arguments.
  • enzotib
    enzotib almost 13 years
    Not what I would to hear, anyway thanks for the info
  • enzotib
    enzotib almost 13 years
    Thank you, interesting. As "standard" I mean something provided by the DE and conforming to freedesktop.org.
  • Drew
    Drew over 12 years
    WoW this is still a bug, lots of progress in xdg. exo-open is listed as workaround and it opens gedit too. :(
  • pabst
    pabst about 12 years
    I 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
    daisy about 12 years
    You 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
    Hamish Downer almost 12 years
    @warl0ck: thanks for that - I've updated the answer with your suggestion.
  • Flimm
    Flimm over 11 years
    If you have the reputation, the most appropriate action would be to edit that answer, actually.
  • Flimm
    Flimm over 11 years
    Strike that, anyone can suggest edits, even users who haven't logged in! It's no biggie any way.
  • Bruno Pereira
    Bruno Pereira over 11 years
    This is an answer by itself, its fine.
  • sdaau
    sdaau about 11 years
    Thanks for the code - I'm on Lucid, and I simply saved this as /usr/bin/xdg-openpy, and gave it a chmod +x - and used launcher.launch([],context) instead of ...None,context) (because of "TypeError: argument 1: Must be sequence, not NoneType"). Now xdg-openpy app.desktop works from command line (and all as normal when double-clicking app.desktop), and it can remind me if I try to call in terminal xdg-open and press tab. Cheers!
  • MestreLion
    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 modern gi.repository instead of the deprecated pygtk, so great! :)
  • MestreLion
    MestreLion almost 11 years
    @RichardHolloway: gnome-opendoes not call xdg-open, it's the other way around! So the issue lies in gvfs-open (the successor or gnome-open)
  • MestreLion
    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
    MestreLion almost 11 years
    you can use "${@:1}" instead of shift, but that requires bash instead of sh in your #! shebang. IMHO your original shift approach is simpler and better
  • Ingo Leonhardt
    Ingo Leonhardt almost 11 years
    In 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
    Lucio almost 11 years
    It doesn't show the icon of the application. Is this an expected behaviour or I should have something wrong in some file?
  • Albert
    Albert almost 11 years
    This didn't worked for me. It seems any paths are double quoted. The script by Carlo worked.
  • jmtd
    jmtd over 10 years
    Upvote for awk rather than a chain of greps and seds.
  • Shirish Herwade
    Shirish Herwade over 10 years
    awesome answer... worked for me
  • Sam Watkins
    Sam Watkins almost 10 years
    this is so stupid (that there is no sensible way to run a Desktop file from the terminal)
  • Sam Watkins
    Sam Watkins almost 10 years
    only good answer
  • jarno
    jarno over 9 years
    Yes, just tested it in Ubuntu Studio 14.04. BTW. xdg-open and gvfs-open work there, too.
  • basic6
    basic6 about 9 years
    Upvoted because it works. No need to be running KDE at all, as long as you have this program installed.
  • Admin
    Admin about 9 years
    This is the way, works in debian too.
  • Croad Langshan
    Croad Langshan about 9 years
    gtk-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
    Noitidart about 9 years
    This bug is still there huh?
  • Noitidart
    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
    Noitidart about 9 years
    I 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
    Ingo Leonhardt about 9 years
    @noitidart unfortunately not, I've learned to live with it and haven't made any more efforts
  • Noitidart
    Noitidart about 9 years
    Thanks @IngoLeonhardt for the reply! I'm actually finding lots of documentation on Gio and it has a lot of stuff mentioning icon (such as gdk_app_launch_context_set_icon_name) I think this is very possible! Did you explore the Gio docs? I'm reading code here: github.com/mathitme/nautilus/blob/…
  • Ingo Leonhardt
    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
    Noitidart about 9 years
    Will definitely keep you updated @IngoLeonhardt :) I badly need the custom icon solution :)
  • Noitidart
    Noitidart about 9 years
    Hey @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
    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 using gdk_app_launch_context_set_icon_name with Gio launch_uris, maybe it will make it take the icon, any idea?
  • Ingo Leonhardt
    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
    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
    Noitidart about 9 years
    xseticon looks interesting it must be a custom function which sets _NET_WM_ICON
  • Noitidart
    Noitidart about 9 years
    @IngoLeonhardt yep XChangeProperty is how they do it i downloaded that file: gist.github.com/Noitidart/64ad2edbda32627b684f
  • Jonathan
    Jonathan almost 9 years
    Works in 15 as well
  • A.B.
    A.B. over 8 years
    But the awk command is nice. Therefore a +1
  • A.B.
    A.B. over 8 years
    Nice, awk and Serg =)
  • A.B.
    A.B. over 8 years
    And sometimes, there is a TryExec, maybe you should check your 2nd command =)
  • Sergiy Kolodyazhnyy
    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 to C. Oh, and already added TryExec part ^_^
  • A.B.
    A.B. over 8 years
    You have my +1 =)
  • A.B.
    A.B. over 8 years
    But what's about %f, %u, %U or something like this behind the command?
  • Sergiy Kolodyazhnyy
    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 have Exec=firefox %u, then $1 is Exec , separator is = and $2 is firefox %u
  • A.B.
    A.B. over 8 years
    Ah sorry, my mistake :\
  • Kwaadpepper
    Kwaadpepper over 8 years
    Is 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
    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
    Alicia about 8 years
    Yay! An answer that works!
  • Axel Beckert
    Axel Beckert almost 8 years
    IMHO 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
    Stuart Axon over 7 years
    Perfect! 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
    Samuel about 7 years
    This 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
    king_julien about 7 years
    Using Debian with xfce. Works great!
  • Victor
    Victor almost 7 years
    Works for me on Arch Linux, but maybe it's an Ubuntu-specific bug.
  • Boris Churzin
    Boris Churzin over 6 years
    Upvote, although it doesn't work (or better put - works too much) if you have ultiple Exec= lines :/
  • gerardw
    gerardw over 6 years
    Works for me in Ubuntu 16.04
  • nhed
    nhed almost 6 years
    hackish hack is hacky - need solution that causes the desktop to run it
  • wisbucky
    wisbucky almost 6 years
    Doesn't work for me in Ubuntu 16.04. Just opens the .desktop file in gedit.
  • MountainX
    MountainX over 5 years
    This 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
    MountainX over 5 years
    This 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
    MountainX over 5 years
    I 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
    Joseph Garvin over 5 years
    No 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
    Joseph Garvin over 5 years
    What 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
    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
    Tyler Collier over 5 years
    Works for me in Ubuntu 14.04
  • MarSoft
    MarSoft about 5 years
    This 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 And dex solution works fine.
  • tatsu
    tatsu about 5 years
    same ubuntu 18.10 still opens gedit
  • tatsu
    tatsu about 5 years
    well 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
    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
    itmuckel about 5 years
    Works for me in 18.04 using Xubuntu. According to the help output of xdg-open and xdg-mime it shouldn't.
  • Bob C
    Bob C over 4 years
    This 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
    jarno over 4 years
    The files could be even in any data dir specified in specifications.freedesktop.org/basedir-spec/…
  • kokbira
    kokbira over 4 years
    It solves that issue for Fedora 30 too
  • ricab
    ricab about 4 years
    That's it! Too much scrolling for the right answer...
  • FallenWarrior
    FallenWarrior about 4 years
    Thank 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), and gtk-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
    Penghe Geng almost 4 years
    Works on 20.04.
  • knezi
    knezi over 3 years
    @BobC yep, just change the two last seds to: sed 's/^"\(.*\)"$/\1/g'
  • geekley
    geekley over 3 years
    This doesn't seem to work for files in ~/.local/share/applications/.
  • geekley
    geekley over 3 years
    kioclient5 exec ~/.local/share/applications/myprogram.desktop worked for me on Kubuntu 20.04
  • geekley
    geekley over 3 years
    Ah! kioclient5 exec ~/.local/share/applications/myprogram.desktop worked for me on Kubuntu 20.04. See answer below if you're on KDE.
  • chenchuk
    chenchuk over 3 years
    The only one that worked for me so far. Thanks.
  • shadowtalker
    shadowtalker over 3 years
    Can exo-open be installed as a standalone package on Ubuntu without pulling in all of XFCE? I ask because in Opensuse exo-tools is a standalone package, with only minimal dependencies on XFCE (I think just one core library).
  • Gabriel Staples
    Gabriel Staples about 3 years
    I 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
    Zanna about 3 years
    Does 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
    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
    RatajS over 2 years
    @geekley It worked in KDE Neon 5.22.4 (Ubuntu 20.04) as well.
  • Alexei Martianov
    Alexei Martianov over 2 years
    For 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
    Ivan Marjanovic over 2 years
    No, this is not working
  • Aron Cederholm
    Aron Cederholm about 2 years
    @MountainX, it is located in package kde-cli-tools