10

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
  • 2
    I assume you mean xdg-open which should open the file in your browser. The open command is a link to the openvt command and opens a binary in a new virtual console. That's apparently not what you want. – Marco Jan 5 '16 at 13:43
  • 1
    Assuming you're on an OSX system? I don't believe there's any open command on Linux. – slm Jan 5 '16 at 13:51
  • 1
    @slm, as Marco points out, there is an open command on Linux, linked to openvt; the error messages in the question are consistent with that situation. – Stephen Kitt Jan 5 '16 at 14:02
  • xdg-open filename_or_URL – Ijaz Ahmad Khan Jan 5 '16 at 14:15
  • 1
    @EdgarOliveira If you want to open a program (a binary) just type its name into the terminal. If you want to open a file with an associated program, use xdg-open <file> and it should open the file with the corresponding program. – Marco Jan 5 '16 at 15:25
14

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.

  • but if I ran this command "sublime_text indext.html" I receive this result "sublime_text: command not found" How can I result that problem? – Edgar Oliveira Jan 6 '16 at 23:54
  • @EdgarOliveira I think I see the problem, apparently Sublime Text doesn't come with a proper package? See my edit. – Gilles Jan 7 '16 at 0:05
  • I created an alias for that: alias sublimetext='/opt/sublime_text/sublime_text /home/user/Documents/sandbox/webdeveloper' but when I execute "sublimetext" opened two windows, instead only one! Why? Thanks – Edgar Oliveira Jan 7 '16 at 10:16
  • When I changed the file .bashrc I has the doubt "How reset the file .bashrc to can execute a new alias, on the same terminal where I did the changes?" Google it and found the reply, in the Unix & Linux, and only you have execute command "bash" and you will run the yours new alias :) – Edgar Oliveira Jan 7 '16 at 10:25
7

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.

  • 1
    The only problem is xdg-open will open the file in default application for the filetype. You can't choose the application with xdg-open. I don't know if there is a command to choose the app, but if you know the app (like in the example "Sublime Text") it will be better to invoke the program directly. – X3MBoy Jan 5 '16 at 18:24
  • 1
    @X3MBoy The idea behind the xdg-open call is to invoke the default application for that file. If you want to use a different program, just call <program> <file>. I fail to see the problem. – Marco Jan 5 '16 at 18:41
  • He ask to some command to do: open index.html -a "Sublime Text" - That's why i say "problem", because if you use xdg-open to open a html file, there are bettern chances that th file will be opened by browser not by a text editor like "Sublime Text" – X3MBoy Jan 6 '16 at 2:56
3

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
2

My solution (used on a daily basis) is to use an alias for xdg-open.

Just put this on your ~/.bashrc:

alias open='xdg-open '

Then close and restart your shell or source ~/.bashrc.

Now you can open any file with that command... from .png to .jpg to .docx to .pdf to .txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.