Mac OSX

How to Open Folder, Files or URL on macOS using Terminal

In the macOS Terminal, the open command is used to open files, directories, applications, and URLs using the default application associated with the file type or URL. Here’s the basic syntax of the open command:

open [options] [file | directory | application | URL]

By default if you type open . in the terminal and hit enter, it’s going to open the current folder. Some common options include -a to specify a specific application to open the file with, -g to open the file with its default application but in the background, and -n to open a new instance of the application even if it’s already running. Additionally, you can use wildcards such as * and ? to open multiple files or directories at once. Here is how you can perform some basic functions with open command:

Files

Using open one can run a file with any application, for example let’s open talkofweb.pdf using Google Chrome:

open -a "Google Chrome" talkofweb.pdf

In order to reveal the files or folders in Finder window, use following:

open -R ~/D*

The above D* wildcard will open Finder windows showing every folder which starts with the letter D.

Folders

The command can not only open current folder but it can also open any other folder with the following syntax using the path:

open ~/Library/Preferences
open /etc

In order to open multiple folders use the following:

$ open ~/Desktop ~/Documents ~/Downloads

The ~ in above command is user home folder path which contains the concerned folders.

Applications

Inside terminal open can be used to run any application as following:

open -a Maps
open -a Safari

Similarly, you can pass arguments to open a file using a particular application as follows:

open -a Preview ~/Desktop/TalkOfWeb.png

Websites and URLs

Surprisingly, you can open direct URLs with open command:

open -a "Google Chrome" https://www.talkofweb.com

In order to use the default browser simply remove the word Google Chrome:

open -a https://www.talkofweb.com

Text Editors

In order to edit a txt or any html file using terminal, you can launch a text editor like this:

open -e talkofweb.txt

In case, you don’t want text editor to edit the given file then alternatively you can open it using the default editor using the following:

open -t index.html

In order to pipe command output into a text file, use the following:

$ ls -l ~ | open -tf

The above command will write the output of ls -l ~ command to the default text editor by creating a new file. Basically, ls will list the directory of ~ i-e user folder.

Using open command by passing arguments

In some cases, you might need to pass arguments to the application which you are about to launch. The process is fairly simple:

open -b com.google.Chrome --args --profile-directory='Profile 1'

The -b option identifies the bundle of the app, which in this case is Chrome and ultimately it passes argument Profile 1 so that the browser opens with a specific profile.

Conclusion

The macOS Terminal’s open command provides a powerful and convenient way to launch various files, directories, applications, and URLs directly from the command line. By leveraging the open command, users can quickly access their preferred default applications or specify specific ones for opening different file types or performing specific tasks. In addition, the users can also pass arguments to an application while opening it using the terminal.