Bring the Command Line into the Real World - Take Control of the Mac Command Line with Terminal (2015)

Take Control of the Mac Command Line with Terminal (2015)

Bring the Command Line into the Real World

So far in this book I’ve largely ignored Mac OS X’s graphical interface, treating the command-line environment as a separate world. In fact, because the command-line interface and the graphical interface share the same set of files and many of the same programs, they can interact in numerous ways.

In this chapter, I discuss how your shell and the Finder can share information and complement each others’ strengths—giving you the best of both worlds.

Get the Path of a File or Folder

Suppose you want to perform some command from the command line on a file or folder you can see in the Finder, but you don’t know the exact path of that folder—or even if you do, you don’t want to type the whole thing. You’re in luck: there’s a quick and easy way to get the path of an item from the Finder into a Terminal window.

To get the path of an item in the Finder, do the following:

1. In a Terminal window, type the command you want to use, followed by a space. The space is essential!

2. Drag the file or folder from the Finder into the Terminal window.

As soon as you release the mouse button, Terminal copies the path of the file or folder you dragged onto the command line. It even escapes spaces and single quotation marks with backslashes for you automatically! You can then press Return to run the command.

For example, suppose you want to use the ls -l@ command to list the contents of a folder with their extended attributes (a type of metadata, or extra information about files and folders in addition to their actual contents), which you can’t see in the Finder. You could type this:

ls -l@

(Don’t forget the space after the @!) Then drag a folder into the Terminal window, as shown in Figure 6.

**Figure 6:** Drag a file or folder into the Terminal window (top); when you release the mouse button, you get that item’s full path (bottom).

Figure 6: Drag a file or folder into the Terminal window (top); when you release the mouse button, you get that item’s full path (bottom).

Open the Current Directory in the Finder

On occasion you may be using the command line deep in Mac OS X’s directory hierarchy (whether or not it’s a location that’s normally visible in the Finder) and want to open the current directory as a folder in the Finder.

You can do so with one of the simplest commands ever:

open .

That’s open followed by a space and a period. And that’s all it takes! The single period is Unix for “the current directory”; we’ll see it again later in this book.

Open a Hidden Directory without Using Terminal

If all you want to do is open a directory that’s normally hidden, you need not open Terminal to do so, as long as you know its location. Just choose Go > Go to Folder in the Finder. In the dialog that appears, type the whole path of the directory (Figure 7) and click Go. That directory opens as a folder in the current Finder window.

**Figure 7:** Open almost any directory, even hidden ones, in the Finder using the Go to Folder dialog.

Figure 7: Open almost any directory, even hidden ones, in the Finder using the Go to Folder dialog.

Tip: When you’re typing a path in the Go to the Folder dialog, you can use tab completion just as in the bash shell (see Use Tab Completion); that can save you considerable typing and guessing.

Open the Current Folder in Terminal

Suppose you’re looking at some folder in the Finder and you realize you want to run a command-line program on the items in it, such as one that renames a bunch of files. You could open Terminal and type in the path to the folder, but that can be cumbersome. Wouldn’t it be great if, instead, you could just click a button, choose a menu command, or press a keyboard shortcut and have a new shell session open in Terminal, with the current directory already set to the folder you were just looking at in the Finder?

In fact, you can do exactly that, and I’ll show you two different ways to do so.

Use Services (Mavericks and Later)

Starting with 10.9 Mavericks, OS X includes two commands you can optionally add to the system-wide Services menu. One of these opens a new Terminal window set to the current folder, and the other opens a new Terminal tab set to the current folder.

To enable these, choose System Preferences > Keyboard > Shortcuts > Services. In the Files and Folders category, select New Terminal at Folder, New Terminal Tab at Folder, or both. (With one of these commands highlighted, you can optionally click the Add Shortcut button to add a keyboard shortcut to it as well.) Then close System Preferences.

To use these new commands, right-click (or Control-click) the folder’s name. Depending on how many Services you have enabled, the New Terminal commands will appear either directly on the contextual menu, or on a Services submenu. Choose the command you want to open a new Terminal window or tab at that folder’s location.

Use cdto (Any Version of Mac OS X)

In any version of Mac OS X, you can instead use the free cdto utility written by Jay Tuley. Unlike the services built into Mavericks and later, this utility works even if you don’t have a folder selected.

To install this utility, follow these steps:

1. Download cdto and unzip it if necessary.

2. From the Terminal subfolder of the cdto folder, drag the cd to app to /Applications/Utilities (or wherever you want to keep it).

3. While holding down Command and Option, drag the application from its new home onto the toolbar of any Finder window. (You should see a plus (+) icon appear at your pointer, signifying that the Finder is ready to add a button to your toolbar.) Move your pointer to where you want your new button to appear, and release the button.

From now on, the button (shown in Figure 8 in Yosemite) appears in the toolbar of every Finder window. You can click that button at any time to open Terminal and start a new shell session with the directory preset to your current location.

**Figure 8:** Click the new `cdto` button in the toolbar of any Finder window to open it in a new shell session.

Figure 8: Click the new cdto button in the toolbar of any Finder window to open it in a new shell session.

Open a Mac OS X Application

If you ever need to open a regular Mac OS X app from the command line, you can do it by entering the open command with the -a (application) flag. For example, to open Safari, just enter this:

open -a Safari

The open -a command is amazingly smart. You don’t have to tell it where the application is located; it can be located in /Applications, or in /Applications/Utilities, or anywhere else on your disk—it doesn’t matter. And you need not spell out “Safari.app” or go through any other complicated steps to get to the application.

Open a File in Mac OS X

Similarly, you can open a particular file that you see on the command line in the default Mac OS X application for that file type—or another application. For example, if the current directory contains a graphic named flowers.jpg, you can open it in its default application (probably Preview) like so:

open flowers.jpg

But if you prefer to open it in Adobe Photoshop Elements, just enter this:

open -a Adobe\ Photoshop\ Elements flowers.jpeg

(Note the backslash before each space in the application name; you could also put the application name inside quotation marks.) Don’t forget you can use tab completion to help spell out the names of files and directories, too (but, alas, not the names of applications).