Open a Folder or File from the Terminal

Sometimes when you’re sitting in the terminal, you need to quickly open a file from the current directory in an editor. Or view an image. Or open a PSD file in Photoshop.

And you have to find this folder in the explorer, then the necessary file, then open it. But you can open it with a single command right from the terminal.

Mac OS

The open command.

To open a directory in Finder, pass the directory name. The current directory is a dot:

# Will open the current directory 
open .
Folder opened in Finder
Folder opened in Finder

If you pass a file name, it will be open in the default application. CSS in a text editor, images in Preview, PSD in Photoshop:

# Will open the file in Photoshop
open file.psd
File opened in Photoshop
File opened in Photoshop

If you need to open in a non-default application, use the -a option. My default for JS files is Sublime Text. But I’ll open it in Atom with this command:

open -a 'Atom' file.js
File opened in Atom
File opened in Atom

You can even open a website:

# Open a website
open 'https://isqua.ru/blog/'
Website opened in browser
Website opened in browser

The open command has other options, check them out like this:

open --help

You can also open a directory from Finder in the terminal. Just drag the folder to the terminal icon in the dock. And if you drag it into an open terminal window, the path to that folder will be inserted into the command.

Windows

The start command.

# Will open the directory
start path/to/dir
Folder opened in Explorer
Folder opened in Explorer
# Will open the file in the default editor
start file.js
File opened in editor
File opened in editor
# Will open the file in Notepad
start 'Notepad' file.md
File opened in Notepad
File opened in Notepad
# Will open the address in the browser
start 'https://isqua.ru/blog/'
Website opened in browser
Website opened in browser

The attentive reader noticed that the start command works from cmd, from PowerShell and from bash (cmder). You can get help on the start command in PowerShell or cmd like this:

help start

If you use Cmder or ConEmu, you can also configure opening a folder from Explorer in the terminal.

Linux

The xdg-open command. It can also open folders, files and addresses. Like this:

xdg-open 'https://isqua.ru/blog/'

You can get help on the xdg-open command like this:

man xdg-open

If you don’t know how to open a file in the necessary application from the terminal, just open the folder that contains this file, and then use the mouse. Even that will save you time.