arch bash cakephp conf dauth devops drupal foss git golang information age life linux lua mail monitoring music mysql n900 netlog openstack perf photos php productivity python thesis travel uzbl vimeo web2.0

Opening files automatically on mainstream Linux desktops

Xfce/Gnu/Linux works amazingly well on my moms workstation, with one exception: opening files automatically with the correct program.

The two biggest culprits are:

  • Gtk's "open file with" dialog: if any Gtk program doesn't know how to open a file it brings up this dialog that is horrible to use. You can search through your entire VFS for the right executable. No thumbnails, no usage of .desktop files, $PATH, autocompletion and not even limiting the scope to directories such as /usr/bin
  • Mozilla software such as Firefox and Thunderbird: they only seem to differentiate files by their mimetype, not by extension. There are add-ons to make it easier to edit these preferences, but eventually you're in a dead end because you get files with correct extensions but unuseful mimetimes (application/octet-stream)

Luckily the fd.o guys have come up with .desktop files.
In $XDG_DATA_HOME/applications/defaults.list you can define which applications to use for which mime type.
So the only thing you need to do is call xdg-open to open a file with the application you want.
And that's exactly what I did. For now I just configure everything to use xdg-open and that seems to work just fine. Oddly enough, even though the defaults.list is also based on mimetypes, files that are marked as octet-stream in Thunderbird seem to be properly recognized by xdg-open and opened with the correct program. Or maybe this is because at the time of testing I only had octet-stream .doc files. Anyway, we'll see. But for now it's looking pretty good.

Note: somewhat related and also not very pleasant: epdfview used to say 'encrypted document.. please enter password' whenever you tried to open a non-existent file, a directory,.. basically anything else then a pdf file. But this seems to be fixed since 5 days ago.


This is a useful bit of info, I have been annoyed by this quite a few times.

In my personal experience, Firefox was never able to download and open a pdf file, It would give me that encrypted document thing every time. Eventually I switched to evince over epdfview (seems to render slightly faster for some reason), but I still had to use mozplugger for it to work. Recently when mozplugger was interfering with the gecko-mediaplayer plugin I got rid of it, and noticed that now Firefox does what it's supposed to.

The goofy thing about this is that it requires desktop files in some global directory to work. I can't tell it to run ~/bin/myscript easily, can I?

Well, no, the .desktop files used can also be in $XDG_DATA_HOME/share/applications/ ...

Nice find! This is functionality that I've been missing for a while.

I'm going to try to roll this out onto my machines over the next week - might post again about my experiences

I didn't notice when it happened, but for a while now everything I try to open in Xarchiver, is opened by Firefox and then Firefox asks if I want to open the file with my preferred application for that file type. Xarchiver uses xdg-open.

For example, I have this line in defaults.list: "application/pdf=evince.desktop" but if I make "xdg-open /path/to/pdf.file" the pdf is opened by Firefox that asks if I want to open the file with evince :-/. Any idea why this happens?

PS: I know there's a file where I have a lot of mimetypes <-> application association, this file is read by Firefox. But I don't remember where it is and don't know I useful is this.

- Aaron/Samuel: you can indeed have both global and user-specific .desktop files. (AFAIK all fd.o specs are configurable globally and per-user, where relevant). So you can easily run your own scripts too.
- Jim: please do
- Andre: that's odd. You can use xdg-mime to debug this.
I believe there are some mime related config files in $XDG_CONFIG_HOME or $XDG_DATA_HOME.

Ok, so I was missing the "xprop" command contained in the xorg-utils package. The most interesting part is that after I installed it, xdg-open started to work has I expected but it broke xdg-mime that now always returns the message "xdg-mime: no method available for quering MIME type of 'file name here'".


xdg-open is fine most times, but it always backgrounds the process. That is, if you type "xdg-open filename" in bash, it immediately returns to the prompt and the opener runs in the background.

However some application wait until the opener has finished and then delete that file (e.g. alpine). In that case xdg-open is useless because the file is deleted by the time the opener tries to open it.... Any ideas for that?

Ugh I just looked at xdg-open's source code (it's just sh) and it's looking uglier then I thought.
It tries to detect your DE (kde/gnome/xfce) and falls back to "generic" if none found. It has branches to do different things depending on your DE.
kde -> kfmclient
gnome -> gnome-open
Xfce -> exo-open

In my case it's 'generic' so it just executes the command and waits for it. In the other cases it will wait for exo-open/gnome-open/.. to return, and returns it's exit code.
So I guess in your case the problem is exo-open/gnome-open/.. returning too fast. I don't have any of those tools so I can't help. (any time is a good time to switch to a lightweight tiling WM ;-)

Sorry for the late reply.

It's not terrible to do this, just need to create a myscript.desktop file and associate it with the mimetype. Find something simple in /usr/share/applications as a base and make one with your script as Exec, I belive %F is replaced by the filename.

I used to have one for a custom playnow script to pipe audio through mpd, but I stopped using xdg-open proper a while ago. It's way easier (IMO) to just mask it by putting my own xdg-open script ahead of the default in $PATH, then I have a simple case statement. Could even fall back on extension if I wanted to...

As long as Chromium/Firefox/etc is looking for an xdg-open in $PATH and yours is the first it finds, you can control the behavior however you want.

Yeah I've also realised the xdg-open stuff is more complicated (and limited) then it should be.
A script like yours is an elegant solution.
There's also Xyne's mimeo which is pretty similar:





What is the first name of the guy blogging here?

This comment form is pretty crude. Make sure mandatory fields are entered correctly.
Basic html tags (a,i,b, etc) are allowed, others are sanitized