On macOS, the ‘open’ command is a versatile tool that allows users to open files, directories, URLs, and applications directly from the terminal. It works in much the same way as double-clicking a file in the Finder, opening the item with its default application. This guide provides a detailed overview of the ‘open’ command, explaining its syntax, options, and usage examples.
Basic Syntax of ‘open’
The general syntax of the ‘open’ command is as follows:
open [options] file...
file parameter can be a file name, a directory name, a URL, or a disk image (.dmg). If the ‘open’ command is followed by a file name, macOS will open that file with the default application associated with its file type.
Common ‘open’ Options
Here are some commonly used ‘open’ command options:
-a application: Opens the file or URL with the specified application.
-e: Opens the file with TextEdit.
-f: Reads input from the standard input and opens the result with TextEdit.
- -g: Open application in the background.
-n: Opens a new instance of the application, even if one is already running.
-t: Opens the file with the default text editor.
-R: Reveals the file in the Finder.
-W: Waits until the application finishes running.
Opening Files and Directories
The simplest use of the ‘open’ command is to open a file or directory. For instance, to open a file named ‘example.txt’ in its default application, use:
To open a directory, use:
This command opens the directory in a new Finder window.
Opening Files with Specific Applications
You can use the
-a option to open a file with a specific application. For example, to open a file named ‘example.txt’ with TextEdit, use:
open -a TextEdit example.txt
This works for other types of file as well.
open -a "Quicktime Player" /System/Library/Sounds/Sosumi.aiff
If your application isn’t found, you can use the file path to the application.
open -a /System/Applications/QuickTime\ Player.app /System/Library/Sounds/Sosumi.aiff
The -a option is also useful if macOS doesn’t know which application to use to open the file type.
The ‘open’ command can also open URLs in your default web browser. For example:
Opening Multiple Files
You can open multiple files at once by listing them after the ‘open’ command:
open file1.txt file2.txt file3.txt
Send Terminal Output to TextEdit
An interesting and useful feature of the
open command is its ability to send terminal output directly to TextEdit. This can be particularly helpful when you have long output that you wish to review or save for later use.
You accomplish this with the
-f option. This option tells
open to read from standard input or from a file and open the text with TextEdit.
Let’s look at an example. Say you want to list the files in a directory and send this list to TextEdit. You could use the
ls command piped into
open -f as follows:
ls | open -f
This command will open a new TextEdit window containing the list of files in the current directory.
Similarly, if you want to view the contents of a file in TextEdit, you can use the ‘cat’ command:
cat example.txt | open -f
This command will display the contents of ‘example.txt’ in a new TextEdit window.
-f option reads the input, either from a file or the standard input, and sends this input to TextEdit. It’s a handy tool when you want to manipulate or closely examine command output using TextEdit’s features.
Better Man Pages
Hopefully, you have taken the opportunity to use the man command on some of the commands you’ve seen so far.
If you don’t enjoy using man command, or find it limiting, then we could send the manual to another programme. However starting with Venture, Apple has removed postscript support from Preview and unfortunately man command that macOS comes with is missing the option to output into PDF.
brew install man-db
By default the new command will be prefixed with ‘g’.
If you would like
gman to replace the built in man command, then you will need to update your PATH variable with the man-db bin directory.
Now you are using the gman command, or updated your path to replace the original man command, we can now generate the pdf.
gman -Tpdf zsh %PDF-1.4 %???? troff: <standard input>:326: warning: can't find special character 'u00A0' 3 0 obj << /Contents [4 0 R ]
This might look like gibberish, but this is a code for a PDF. We just need to send it to a programme that understands it.
We will use the -f option to specify we want the open command to send the input of man into the application we open. By default, the
open command will use TextEdit, however that will result in the same code being displayed. The application we need to use is Preview.
gman -tpdf zsh | open -fa /System/Applications/Preview.app
This will open Preview with the man page as a lovely pdf file, complete with page numbers.
From here you can save this file or print the PDF.
The ‘open’ command in macOS is a powerful tool that extends the command line’s capabilities to interact seamlessly with the graphical user interface. It provides the flexibility to open files, directories, and URLs, and to control which applications are used to open them.
By incorporating the
open command into your terminal usage, you can streamline your navigation and file management processes, making the terminal an even more effective tool for managing your macOS environment.