Finding the preferred application for a given file extension via unix shell commands

时光毁灭记忆、已成空白 提交于 2019-12-01 10:11:18
dmckee

On unix per se that would be the one the user used to open it, because there is no OS level notion of a preferred application.

However the major X desktop environment all define such a notion, and then you have to use their facilities:

  • gnome-open in GNOME (duh)
  • exo-open in XFCE [see the comments in the gnome link]
  • xdg-open may work in many environments (reputedly works in KDE) [see the comments in the gnome link]
  • just plain kfmclient exec (or kfmclient4 exec) in KDE (I haven't been able to find a reference to kde-open as Rob H suggests, and don't have a KDE system at hand to try it)

Now Mac OS X provides the open command which works like clicking the file in the finder (which is to say, it asks the OS...)


Several corrections thanks to ephemient in the comments. I won't discuss mailcap, because I never understood it and had forgotten it existed...

The answer differs depending on the desktop environment your using. Since you mentioned Okular, I'm going to assume you're using KDE. So try:

kde-open <file>

For GNOME, there is the equivalent:

gnome-open <file>

To answer this myself, I've defined a simple (bash) function that works in the way I expect:

function show { 
    xdg-open $1 &> NUL
    }

xdg-open was almost exactly what I wanted, but it lets ugly program warnings slip through into the shell, which the above seems to fix.

Thanks all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!