4

File associations usually are made in the desktop environment, but how to get a list of applications associated with a file using command line?

Something like:

$ getassoc foo.pdf <CR>
$ acroread, okular

Does not need to be a command, can a cat + grep on any Gnome file

I'm using Gnome 2.28.2.

  • Just an observation, it's not using the user interface, only command line. – Rodrigo Gurgel Feb 7 '14 at 18:16
4

There really isn't a centralized resource that you can just "query" to get at this information. Rather it's maintained in a couple of text files that you can either manually parse if you know where to look or you can use the tool xdg-mime to construct the relationships.

Example

Say I have a PNG file on disk. I can find out its MIME type like this.

$ xdg-mime query filetype DSCN4747_DSCN4061_800x600.PNG 
image/png

I can then query xdg-mime asking it what the association is for this particular MIME type.

$ xdg-mime query default image/png
shotwell-viewer.desktop shutter.desktop

Looking through the mimeinfo.cache on my system I can find out a bit more about associations for a given MIME type using the following command:

$ grep 'image/png' /usr/share/applications/mimeinfo.cache 
image/png=shutter.desktop;gpicview.desktop;gimp.desktop;eog.desktop;geeqie.desktop;shotwell-viewer.desktop;

The desktop definitions in the mimeinfo.cache file are stored here:

$ locate shotwell-viewer.desktop
/usr/share/applications/shotwell-viewer.desktop

And it contains the name of the executables you're asking about:

$ grep 'Exec=' !$
grep 'Exec=' /usr/share/applications/shotwell-viewer.desktop
Exec=shotwell %f

Often times if I just want to launch something, I'll use the tool xdg-open <file|URL> to open a file rather than go and launch the app first, and then open the file.

References

4

Your local config file

~/.local/share/applications/mimeapps.list

global config file

/usr/share/applications/mimeinfo.cache

Simple layout with sed,

sed -e 's/=/\n\t/' -e 's/;/\n\t/g' /usr/share/applications/mimeinfo.cache

Example output,

...
image/x-xwindowdump*
    gimp.desktop
video/flv
    totem.desktop
    avidemux-gtk.desktop

application/vnd.sun.xml.math
    openoffice.org-math.desktop
application/vnd.scribus
    scribus.desktop
...
1

https://askubuntu.com/questions/16580/where-are-file-associations-stored talks about how associations work and the tools to update them. In Linux the key word you are looking for is "mimetype".

The Freedesktop group which has worked out standards used by KDE, GNOME, and others has tools for inspecting and launching apps based on their mime types. Look at the man page for xdg-mime and xdg-open. These only work within an active desktop session. Keep that in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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