Archives and disk tools
Linux commands: open
Learn how the macOS open command opens a file, a folder in Finder, or an application, and why open . is so handy, with xdg-open as the Linux equivalent.
The open command opens a file from the terminal, using this syntax:
open <filename>
The file opens in the default app registered for that type, exactly as if you had double-clicked it in the Finder. open notes.txt launches your text editor, open photo.png your image viewer.
That is the point of this command. It bridges the terminal and the graphical side of macOS. You’re already in the shell, already in the right folder, and you don’t have to reach for the mouse to look at something.
You can also open a directory, which opens a Finder window on it:
open <directory name>
I use it all the time to open the current directory:
open .
The special
.symbol points to the current directory, as..points to the parent directory
This is the version I type most. I finish something in the terminal, open ., and drag the file wherever it needs to go.
URLs work too. This opens your default browser on the site:
open https://flaviocopes.com
The same command can also launch an application. Pass the -a option followed by the application name:
open -a Calculator
-a is also how you open a file with a specific app instead of the default one:
open -a "Google Chrome" index.html
Notice the quotes. An application name containing spaces must be quoted, or the shell splits it into separate arguments and open looks for an app called Google.
When something refuses to open, read the error message. open missing.txt fails with:
The file /Users/flavio/missing.txt does not exist.
That’s nearly always a typo in the name or the wrong current directory. Run ls to check what’s actually there, then try again. Notice that open prints the full path it tried, which tells you immediately if you’re in the wrong folder.
The
opencommand works on macOS only. Usexdg-openon Linux
xdg-open covers the same jobs on Linux: files, folders, and URLs, each handed to the desktop’s default application. xdg-open . opens the current folder in your file manager, the same way open . does on a Mac. There’s no -a equivalent, though. To pick a specific app on Linux you call it directly, like firefox index.html.
Lesson completed