Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create a script that returns the default app to be used for a given file extension (including the path). I had a look at the file command which can return the mime type and xdg-open which would open a file. But what I'd like to get is

myscript doc



myscript fun


How do I do that?

share|improve this question
up vote 1 down vote accepted

I grabbed a Linux expert in the office and we found a reasonable solution:

CURMINE=$(xdg-mime query filetype $SAMPLENAME)
CURDSK=$(xdg-mime query default $CURMINE)

if [ -f /.local/share/applications/$CURDSK ]; then
elif [ -f /usr/local/share/applications/$CURDSK ]; then
elif [ -f  /usr/share/applications/$CURDSK ]; then
    echo "Sorry no executable found for $1"
    exit 1

WHATTODO=$(grep "^Exec" $TRUEDSK | head -1)

Once we figured that there are only 3 locations for the desktop files it was not hard anymore.

share|improve this answer

The command can be something like:

xdg-mime query default `xdg-mime query filetype example.odp` 


share|improve this answer
Hi Samik, thx for the reply. I tried the command on Ubuntu 12.04 and ran into 2 problems: a) if example.odp doesn't exist it just throws an error and secondly (the bigger issue) I just got application/vnd.oasis.opendocument.presentation in return. Also the desktop file doesn't execute, I need to have something I can call with a set of parameters thereafter – stwissel Jul 5 '12 at 0:44
Quick update: I can get to the desktop file using xdg-mime query filetype example.odp || xdg-mime query default but then still the challenge is how to get to the executable including the path – stwissel Jul 5 '12 at 0:49

Your Answer


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.