How to use the ''open'' command in OS X

From ArticleWorld


Unlike Apple's previous operating systems, OS X has a strong Unix heritage through its foundation, Darwin. Darwin is a Unix-class operating system, abiding to POSIX standards and following a BSD model in many of its aspects.

Therefore, besides the Aqua GUI, OS X offers a number of strong command-line tools. You can run native CLI applications by simply typing their name. But what about Cocoa applications, the one you launch using Finder?

Open

This is where the open command comes handy. Open is a very powerful command, which you can use, as you will see, not just to run applications, but in general to "open" files.

  1. To open a folder in Finder, you simply need to pass the its name to open as an argument. For example, to open a Finder window in the current directory, you would use:
open .

You can open any folder like this, but do remember that, if the folder is not in the current directory, you will need to pass its entire path. For example:

open /System/Library

Remember that ~ denotes your home folder. Therefore, opening your Pictures folder can be done with:

open ~/Pictures
  • Opening applications is just as easy. You simply need to type:
open -a [application name]

obviously replacing [application name] with the actual name. For example:

open -a Chess

will open Chess.

You need to be careful, though. Although you can generally open an application by giving just its name, there are some exceptions regarding this behavior. More precisely:

  • You can only do this if the folder that contains it is present in your PATH variable. You can check if it is by running:
echo $PATH
  • If you have two two applications whose executable files have the same name, this will not work (unless one of them is in the current folder, for obvious reasons).

In both of these cases, you will have to specify the complete path, for example:

open -a /opt/some-application/salauncher