1

How can I without any manual GUI use check what will open file with a specific file extension - for example .rb?

Not a duplicate of how can I change file association globally? as mine is about programmatically reading, not changing in any way, including manual through GUI (though https://askubuntu.com/a/289339/349903 hints that parsing some set of config files may give an answer)

To avoid XY problem: I have system installation script, that works fine except that Okular ends associated with .rb, .py, .txt and other text files, instead of text editor. I want to rerun installation script and log what is now opening specific file extensions after every step. I hope that will allow me to debug what is going wrong.

  • The problem is that file associations are based on mimetype, not file extension. If you know the mimetype(s) in question then you may be able to use xdg-mime to query the default association ex. xdg-mime query default text/plain – steeldriver Nov 10 '19 at 19:12
  • 1
    touch test.rb + mimetype test.rb gives me test.rb: application/x-ruby what allows me to run xdg-mime query default application/x-ruby Are you interested in making an answer? – reducing activity Nov 10 '19 at 20:27
  • 1
    As much as it's great that you're checking other sites for your answer, you don't have to defend your question as "not duplicate" against questions on other sites. There isn't really such a thing as a duplicate question across sites, only questions that are duplicate on the same site (real duplicate), or off-topic questions more appropriate to another site (which will be transferred by a mod). I only mention this because the defence is actually distracting to the question, which is assumed to be on Ubuntu anyway. – tudor -Reinstate Monica- Nov 10 '19 at 23:56
  • @tu-ReinstateMonica-dorduh I removed pointless part – reducing activity Nov 12 '19 at 22:51

1 Answers1

5

To sum up the comments above the answer to this problem is a two step process:

First determine the MIME type of the file:

MIMETYPE=$(xdg-mime query filetype "<your-file-here>")

Then get the default application associated with this type:

xdg-mime query default "$MIMETYPE"

So the mime type could be text/x-python for example and the application would be gedit.desktop on my system.

You can also make this a one liner:

xdg-mime query default "$(xdg-mime query filetype '<your-file-here>')"
mark
  • 398