Linux commands: open
By Flavio Copes
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.
On macOS, the open command lets you open a file using this syntax. On Linux the equivalent is xdg-open, covered at the end of this post.
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 see something.
You can also open a directory, which on macOS opens the Finder app with the current directory open:
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
URLs work too. This opens your default browser on the site:
open https://flaviocopes.com
The same command can also be used to run 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
Note the quotes: an application name containing spaces must be quoted, or the shell splits it into separate arguments.
When something refuses to open, read the error message. open missing.txt fails with The file /Users/flavio/missing.txt does not exist. — nearly always a typo in the name or the wrong current directory. Run ls to check what’s actually there, then try again.
The
opencommand works on macOS only. Usexdg-openon Linux.
xdg-open is the desktop equivalent on most Linux setups. Same idea: hand a path or URL to the default app.
xdg-open notes.txt
xdg-open .
xdg-open https://flaviocopes.com
On some systems you may also see gio open or a distro-specific wrapper. xdg-open is the portable choice for scripts that must run on any Linux desktop.
Want me to talk about your product? You can sponsor this site.