`open` command to open a file in an application
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.
You don't mention the operating system you're using.
On some Linux distributions the open
command is a symbolic link to the
openvt
command which opens a binary in a new virtual console. That's
apparently not what you want. Your error messages indeed indicate that you
invoked the openvt
command. This command expects a program binary (an
executable) as argument.
I assume you mean xdg-open
which, in contrast to openvt
, does not expect an
executable as argument, but a file which is associated with a program and
opens the file with the program. You can pass files and URLs as arguments.
Therefore xdg-open index.html
should open the file in your browser,
xdg-open image.png
should open the file in your image viewer, etc.
Although people often name xdg-open
as emulating the functionality of open
, macOS open
is a bit different because it 1. does not redirect anything from stdout/stedrr and 2. detaches whatever you want to run from the terminal (which is almost always what you want when launching GUI apps so that you don't have terminals laying around)
To solve this, I've come up with a proper equivalent using some standard GNU tools (the syntax is fish shell, but the conversion to bash is trivial):
function open
for i in $argv
setsid nohup xdg-open $i > /dev/null 2> /dev/null
end
end
Added:
For those using the bash shell, here is the function:
open ()
{
for i in $*
do
setsid nohup xdg-open $i > /dev/null 2> /dev/null
done
}