i Open Files From the Command Line in OSX · Dark Matter Industries

Open Files From the Command Line in OSX

On your Mac, you have files that can be opened by certain applications. For instance .pdf files by the Preview.app, .pages by the Pages.app etc. Did you know that you can open these files using the command line as well? This has been around since NextStep.

Files can have a default app to open them with. macOS keeps a database about which app opens what type of files through LaunchServices. So PDFs are open by the Preview app by default.

So, to use the command line:

$ open MyFile.pdf

The open program is very versatile – just glance at its man page. If you don’t want to use Preview but PDFExpert.app, just use the -a flag:

$ open -a /Applications/PDFExpert.app MyFile.pdf

You can also use open to launch a URL:

$ open https://apple.com

and even open a directory in the Finder:

open /Path/To/Folder

or reveal a file in Finder:

open -R /path/to/file