`open` command to open a file in an application

command linefile opening

Why can't I run this command in my terminal:

open index.html

Wasn't it supposed open this file on my browser? Also can't I run this command: open index.html -a "Sublime Text". The result of these commands are:

$ open index.html
Couldn't get a file descriptor referring to the console

$ open index.html -a "Sublime Text" - 
open: invalid option -- 'a' 
Usage: open [OPTIONS] -- command

Best Answer

The primary purpose of OS X's open command is to open a file in the associated application. The equivalent of that on modern non-OSX unices is xdg-open.

xdg-open index.html

xdg-open doesn't have an equivalent of OSX's open -a to open a file in specific application. That's because the normal way to open a file in an application is to simply type the name of the application followed by the name of the file. More precisely, you need to type the name of the executable program that implements the application.

sublime_text index.html

Linux, like other Unix systems (but not, as far as I know, the non-Unixy parts of OS X) manages software by tracking it with a package manager, and puts individual files where they are used. For example, all executable programs are in a small set of directories and all those directories are listed in the PATH variable; running sublime_text looks up a file called sublime_text in the directories listed in PATH. OS X needs an extra level of indirection, through open -a, to handle applications which are unpacked in a single directory tree and registered in an application database. Linux doesn't have any application database, but it's organized in such a way that it doesn't need one.

If running the command sublime_text shell doesn't work for you, then Sublime Text hasn't been installed properly. I've never used it, and apparently it comes as a tar archive, not as a distribution package (e.g. deb or rpm), so it's possible that you need to do an extra installation step. It's really the job of the makers of Sublime Text to make this automatic, but if they haven't done it, you can probably do it yourself by running the command

sudo -s …/sublime_text /usr/local/bin

Replace by the path where the sublime_text executable is, of course.

The open command you encountered is an older name for the openvt command (some Linux distributions only include it under the name openvt). The openvt command creates a new virtual console, which can only be done by root and isn't used very often in this century since most people only ever work in a graphical window environment.

Related Question