Terminal

Open a File From Terminal on Mac: The open Command

Nemo · · 6 min read

To open a file from Terminal on a Mac, type open followed by the file’s path and press Return, for example open report.pdf. macOS opens it in the same app a double-click in Finder would use. To pick the app yourself, add -a and its name: open -a Preview report.pdf.

The open command is the bridge between the command line and the rest of macOS. It doesn’t read the file itself. It hands the file to Launch Services, the part of macOS that decides which app opens what, exactly like Finder does.

Why use Terminal to open a file at all?

You are already in a folder in Terminal, you just ran a script that wrote output.csv, and now you want to look at it. Switching to Finder means finding that folder again. Typing open output.csv lands on the same file without leaving the keyboard.

It also helps when Finder hides things: files that start with a dot, folders inside app bundles, or paths you only know from an error message.

What are the most useful open commands?

Every command below runs in the built-in Terminal app, iTerm2, or any other Mac terminal. Run them from the folder the file is in, or give the full path.

Command What it does
open file.pdf Opens the file in its default app
open -a Preview file.png Opens it in a named app
open -e notes.txt Opens it in TextEdit
open -t notes.txt Opens it in your default text editor
open -R file.pdf Reveals it in Finder, selected, without opening it
open . Opens the current folder in a Finder window
open https://example.com Opens the address in your default browser
open -n -a Preview file.png Starts a new copy of the app even if one is running
open -g file.pdf Opens it without bringing the app to the front
open -W file.txt Waits until you quit the app before the prompt comes back

You can pass several files at once, and wildcards work too: open *.png opens every PNG in the folder.

How do you open a file in a specific app?

Use -a with the app’s name as it appears in the Applications folder, without .app:

open -a "Visual Studio Code" app.js
open -a TextEdit config.yml
open -a Safari index.html

Put the name in quotes when it has spaces. If you see Unable to find application named, check the exact spelling in Applications. You can also use the app’s bundle identifier with -b, for example open -b com.apple.TextEdit notes.txt, which keeps working if an app is renamed.

How do you handle file names with spaces?

Wrap the path in quotes, or put a backslash before each space:

open "Quarterly Report.pdf"
open Quarterly\ Report.pdf

An easy way to avoid typos is to let the Mac write the path for you. Type open with a space at the end, then drag the file from Finder into the Terminal window. The full path appears, already escaped. Pressing Tab after the first letters of a name completes it too.

How do you open a file inside the terminal instead?

open always hands the file to another app. To read or edit it without leaving the terminal, use a command-line tool:

  • cat notes.txt prints the whole file.
  • less notes.txt shows it a page at a time. Press Space to go down and q to quit.
  • nano notes.txt edits it in a simple editor. Ctrl+O saves, Ctrl+X quits.
  • vim notes.txt edits it in Vim. Type :wq and Return to save and quit.
  • qlmanage -p photo.jpg shows a Quick Look preview, the same as pressing Space in Finder.

Most editors also ship a command of their own. VS Code adds code once you run “Shell Command: Install ‘code’ command in PATH” from its Command Palette, and then code . opens the whole folder.

Why does open say the file does not exist?

The error The file /Users/you/report.pdf does not exist. usually means one of three things:

  1. You are in a different folder. Run pwd to see where you are and ls to see what is there. Then cd to the right folder or use the full path.
  2. The name has a space or a capital letter you missed. File names in Terminal are typed exactly. Use Tab completion.
  3. The file is hidden. Names that start with a dot don’t show in Finder by default; ls -a lists them. See how to show hidden files on Mac.

If you don’t know where the file is at all, find it first: mdfind -name report.pdf searches the Spotlight index from the terminal. How to find a file on Mac covers the other ways.

What other Terminal commands go with open?

open is one of a handful of commands worth knowing by heart: cd, ls, pwd, mv, cp and mdfind cover most day-to-day work with files. The full list, with what each one does, is in Mac Terminal commands. If you haven’t opened Terminal before, start with how to open Terminal on Mac.

Can the terminal sit next to the file you are working on?

Most of the time you open a file from Terminal because you were doing something in a browser and a terminal at once: reading docs while running a build, checking a page while editing its config. That means two windows, and a lot of Cmd+Tab.

Monok is a small Mac browser with a native terminal built in, as a pane next to the page. Ctrl+splits a terminal in below the page, running your own login shell, soopen, less` and every command above work there the same way. File › Open Folder in Terminal starts one in a folder you pick. Monok is an early preview, and the terminal is native: no web page or JavaScript can reach it. More in a browser with a built-in terminal.

Next step

Open Terminal, cd into a folder you use every day, and run open -R on one file to see it land in Finder. If you’d like that terminal next to your pages instead of in another window, download Monok.

FAQ

How do I open a file in Finder from Terminal?

Use open -R path/to/file. It opens a Finder window with the file selected, without opening the file. open . opens the current folder in Finder.

How do I open a file with TextEdit from Terminal?

Run open -e file.txt. To use whatever text editor you set as the default, run open -t file.txt instead.

How do I open an app from Terminal on Mac?

Run open -a followed by the app’s name, for example open -a Safari. Without a file, it just launches the app.

How do I open a file from Terminal with a path that has spaces?

Wrap the path in quotes, like open "My File.pdf", or escape each space with a backslash. Dragging the file into the Terminal window writes the escaped path for you.

What is the difference between open and cat?

open hands the file to an app, the same as a double-click in Finder. cat prints the file’s contents in the terminal itself and works only for text.

More in Terminal

Contact

Bug reports, sites you want to see working, ideas. We reply by email.