Open Files from Command Line on OS X

By  on  

I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I can understand what's under the hood of my favorite utilities.

One common case for running any app is opening an existing file, like an HTML file in a browser or a document in Pages.  To open a file on Mac OS X from the command line, type the following:

open portrait.png

The above does not specify an app to open the given file in -- the default app for that file type will be used.  To open a file in a specific app, use the -a argument:

open -a /Applications/Firefox.app portrait.png

You can also open a URL directly from the command line:

open https://davidwalsh.name/

You'd expect opening a file from command line to be easy and I'm happy to confirm to you that it is.

Recent Features

Incredible Demos

  • By
    Create Your Own Dijit CSS Theme with LESS CSS

    The Dojo Toolkit seems to just get better and better.  One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes.  The move to using LESS is a brilliant one because it makes creating your own Dijit theme...

  • By
    Comment Preview Using MooTools

    Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools. The XHTML You can set up your XHTML any way you'd like.

Discussion

  1. I was just wondering how to do this, thanks!

  2. Eric

    When you use -a, you don’t need to write the path.

    open -a Firefox portrait.png

    …would do the same. :)

    The -a handler is great when you want to open some program one doesn’t really know (eg remember) exactly where it is.

    Like:

    open -a "Wi-Fi Diagnostics"

    // that one is in /System/Library/CoreServices

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!