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?

link|improve this question

2  
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. – Bruce Connor Oct 4 '10 at 22:41
feedback

3 Answers

up vote 11 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.

link|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. – Bruce Connor 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. – Bruce Connor 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 at 6:20
@warl0ck: thanks for that - I've updated the answer with your suggestion. – Hamish Downer May 2 at 8:36
feedback

The answer should be

xdg-open program_name.desktop

But due to a bug this no longer works.

link|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. – Bruce Connor 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
feedback

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

link|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 at 20:14
feedback

Your Answer

 
or
required, but never shown

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