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
    Pure CSS Slide Up and Slide Down

    If I can avoid using JavaScript for element animations, I'm incredibly happy and driven to do so.  They're more efficient, don't require a JavaScript framework to manage steps, and they're more elegant.  One effect that is difficult to nail down with pure CSS is sliding up...

  • By
    CSS calc

    CSS is a complete conundrum; we all appreciate CSS because of its simplicity but always yearn for the language to do just a bit more. CSS has evolved to accommodate placeholders, animations, and even click events. One problem we always thought...

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!