How do get an html file to open in preferred html application from command line on linux?

How do get an html file to open in preferred application from command line on linux?

I have an xterm window open and an html file I want to do something like

open test.html

and that will start my preferred browser with the test.html file loaded.

(I’m looking for a solution that will work for any Linux desktop machine without me needing to know which browser app they have installed.)

Asked By: Paul Taylor

||

That’s OS/distribution/desktop-environment dependent, but in many places, you’ll find a xdg-open command that does just that.

Answered By: Stéphane Chazelas

(see https://bbs.archlinux.org/viewtopic.php?pid=732285#p732285)

1.) save some webpage (via “File > Save page as”), e.g. as /tmp/somewebpage.html

2.) find out the media-type/filetype of this file:
xdg-mime query filetype /tmp/site.html (-> text/html)

2.) find the currently assigned default app for this filetype:
xdg-mime query default text/html (-> e.g.: brave.desktop)

3.) define the new default app for this filetype:
xdg-mime default firefox.desktop text/html

Look for “.desktop”-files, e.g. in:

/usr/share/applications/firefox.desktop
/usr/share/xfce4/helpers/firefox.desktop

An(other) Ubuntu/Debian(based) option (maybe this also works for html file, I currently cant verify)

update-alternatives --get-selections (to list apps and assigned command)
update-alternatives --config ... (insert an item from the list into “…”, e.g.: editor)
update-alternative --all (interactively define selection for each option)

There is a gui-tool for that: GAlternatives (https://packages.debian.org/search?keywords=galternatives)

Answered By: MacMartin
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.