Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 exectuable 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?

share|improve this question
4  
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. –  Daenyth Oct 4 '10 at 21:11
    
Interesting, I was wondering why it caused the tab to close. –  Malabarba Oct 4 '10 at 22:41
    
Related: Hashbang for Gnome .desktop files –  ændrük May 24 '11 at 22:49
    
I see, they end up parsing the .desktop file. Thank you anyway for the link. –  enzotib May 24 '11 at 22:53

11 Answers 11

up vote 20 down vote accepted

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/%.//'` &

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
`...`                           - 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/%.//'` &

Then make it executable

chmod +x ~/bin/deskopen

And then you could do, eg

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

The arguments (%u, %F etc) are detailed at http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html#exec-variables - none of them are relevant for launching at the command line.

share|improve this answer
    
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 Oct 4 '10 at 15:18
    
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. –  Malabarba Oct 4 '10 at 15:20
    
I've updated my answer with the extra sed - I forgot the desktop files could have arguments. –  Hamish Downer Oct 4 '10 at 17:10
    
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 .. –  warl0ck Apr 28 '12 at 6:20
1  
-1: This may work for simple .desktop files, but it ignores entries like Path= and TryExec= which may affect execution. It also executes the wrong Exec= if the file contains Desktop Actions ("quicklists") –  MestreLion May 23 '13 at 0:36

The answer should be

xdg-open program_name.desktop

But due to a bug this no longer works.

share|improve this answer
    
On GNOME you can use gnome-open which in turn uses xdg-open which has the bug. –  Richard Holloway Oct 4 '10 at 15:36
    
Damn =/. Guess that solves it. –  Malabarba Oct 4 '10 at 15:44
    
WoW this is still a bug, lots of progress in xdg. exo-open is listed as workaround and it opens gedit too. :( –  Drew Oct 17 '11 at 17:59
    
@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 May 23 '13 at 0:46
5  
"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 May 23 '13 at 0:47

As of today (12.10) the bug is still present. 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, *files):
    launcher = Gio.DesktopAppInfo.new_from_filename(desktop)
    launcher.launch(files, 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
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|improve this answer
    
This works better than Hamish Downer's answer as it will deal with multiple Exec= lines and % parameters in the command correctly. –  Flimm Jan 11 '13 at 9:31
    
@Flimm Glad it is useful. Thanks for the edit. –  Carlo Pellegrini Jan 11 '13 at 12:28
3  
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! –  sdaau Feb 12 '13 at 19:40
3  
+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 May 23 '13 at 0:39
1  
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? –  Ingo Leonhardt May 24 '13 at 14:23

There isn't currently an application that does what you describe in the Ubuntu archives. There are a couple efforts in progress to create a general solution to provide integration for desktop environments (like openbox) that are not compliant with these XDG specifications.

Arch Linux is working on an implementation of xdg-autostart based on the python-xdg libraries. From what I can find, this seems not yet entirely complete, but has some reports of success.

There is also a C++ implementation of xdg-autostart on gitorious (http://gitorious.org/xdg-autostart/) which would likely benefit from wider use.

If either solution works for you, please consider submitting the necessary work for inclusion in either Debian or Ubuntu.

To use either tool with openstart, you would call it in /etc/xdg/openbox/autostart.sh (if I am reading the openbox documentation correctly). If this doesn't work, you can probably call it in any of the openbox session initialisation scripts.

share|improve this answer
    
Not what I would to hear, anyway thanks for the info –  enzotib Jul 6 '11 at 16:09

I don't have an immediate solution meeting the requirement for "using a standard command", but if you did want to minimally parse the .desktop files or wanted to create a Bash alias, then the following should work:

  • awk -F= '/Exec=/{system($2)}' foo.desktop

another approach that might be interesting, would be to create a kernel-level binfmt-misc method than matches on .desktop files (see grep -r . /proc/sys/fs/binfmt_misc/ for those patterns that you currently have enabled).

At the end of the day, something somewhere will have to parse the .desktop files, it's just a question of how "standard/default" that is.

share|improve this answer
    
Thank you, interesting. As "standard" I mean something provided by the DE and conforming to freedesktop.org. –  enzotib Jul 10 '11 at 8:48
    
Upvote for awk rather than a chain of greps and seds. –  jmtd Oct 21 '13 at 10:57

Addendum 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:

#!/usr/bin/env deskopen

Then flag the .desktop file as executable (e.g. with a chmod +x whatever.desktop), and then you can

path/to/whatever.desktop

and 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:

#!/bin/sh
desktop_file=$1
shift
`grep '^Exec' "${desktop_file}" | sed 's/^Exec=//' | sed 's/%.//'` "$@" &

As an aside, I tried using "#{@:2}" instead of shifting, but it kept giving me 'bad substitution'...

share|improve this answer
    
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! –  pabst Feb 9 '12 at 20:14
    
If you have the reputation, the most appropriate action would be to edit that answer, actually. –  Flimm Sep 13 '12 at 12:58
    
Strike that, anyone can suggest edits, even users who haven't logged in! It's no biggie any way. –  Flimm Jan 11 '13 at 9:33
    
This is an answer by itself, its fine. –  Bruno Pereira Jan 11 '13 at 9:34
    
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 –  MestreLion May 23 '13 at 1:00
exo-open [[path-to-a-desktop-file]...]

seems to work in 13.10 release, if exo-utils is installed (like is the case with Xubuntu).

share|improve this answer

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

gtk-launch <file> where is the name of the .desktop file without the .desktop part

So gtk-launch foo opens foo.desktop

Usable from terminal or alt+F2 (alt+F2 stores command in history so easily accessible)


Run gtk-launch <file> /path/to/directory to launch Desktop-file: <file> from Directory: /path/to/directory.

share|improve this answer

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>

share|improve this answer

When trying to test these files I found the simplest way to check that that the DM or session manager would do what I expected was to open the surrounding dir in a UI folder browser and then double-click to open them.

If you are in a command line : gvfs-open . or gnome-open . will open it in the configured folder browser.

The sed thing will not mirror the DM's behaviour, including fiddly stuff like escapes and quoting where you really wouldn't want alternative behaviour. It's not command line, but it did validate things. I also found setting Terminal=true useful for debugging.

share|improve this answer

This SO answer is what made it clear for me: don't try to execute the desktop file, execute the file pointed to in the desktop file.

For example, execute /home/jsmith/Desktop/x11vnc.sh

Exec=/home/jsmith/Desktop/x11vnc.sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.