`open` command to open a file in an application

62,570

Solution 1

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.

Solution 2

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.

Solution 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

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
}

Solution 4

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.

Share:
62,570

Related videos on Youtube

Edgar Oliveira
Author by

Edgar Oliveira

Updated on September 18, 2022

Comments

  • Edgar Oliveira
    Edgar Oliveira almost 2 years

    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
    
    • Marco
      Marco over 8 years
      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.
    • slm
      slm over 8 years
      Assuming you're on an OSX system? I don't believe there's any open command on Linux.
    • Stephen Kitt
      Stephen Kitt over 8 years
      @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.
    • Ijaz Ahmad
      Ijaz Ahmad over 8 years
      xdg-open filename_or_URL
    • Edgar Oliveira
      Edgar Oliveira over 8 years
      Thanks, yes I saw this command on OSX system. In Linux I should use this command "xdg-open . = to open current directory" and xdg-open index.html = to open file in browser." Thanks! But If I want open the Sublime Text or another program through terminal how can I do this?
    • Marco
      Marco over 8 years
      @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.
    • Digital Trauma
      Digital Trauma over 8 years
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 8 years
      @chaos That's not a duplicate of unix.stackexchange.com/questions/244970/… because this question is also about opening in a specific application.
  • X3MBoy
    X3MBoy over 8 years
    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
    X3MBoy over 8 years
    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"
  • Edgar Oliveira
    Edgar Oliveira over 8 years
    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?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @EdgarOliveira I think I see the problem, apparently Sublime Text doesn't come with a proper package? See my edit.
  • Edgar Oliveira
    Edgar Oliveira over 8 years
    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
    Edgar Oliveira over 8 years
    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 :)