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

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

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!