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
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Styling CSS Print Page Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...

  • By
    MooTools & Printing – Creating a Links Table of Contents

    One detail we sometimes forget when considering print for websites is that the user cannot see the URLs of links when the page prints. While showing link URLs isn't always important, some websites could greatly benefit from doing so. This tutorial will show you...

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!