Getting started: OSX Command line interface (CLI)

The Command line interface (CLI) is an incredibly powerful tool in your development arsenal and this first post in a series aims to get you up to speed while minimising the chance of serious damage. The terminal is your friend and your window onto the CLI, however it’s a friend that takes everything you say literally so be very careful what you ask it to do.

Opening a terminal

The easiest way to open a standard terminal is via spotlight. Command + Space will bring up spotlight then type Terminal

My personal preference is to use a free third party application called TotalTerminal which enables access to the terminal via a hot key combination (default CNTRL + ` ) and reveals it in a rather nifty slide and fade in effect. In spotlight Command + Space its accessible via TotalTerminal.

TotalTerminal is ace

Tip: If you work with source control (and you should) then having your terminal window open in your work folder can save some time. You can do this by running a command when the terminal window opens we’ll come back to this a little later when we get to navigating but you set the command to run in the Settings pane of the terminals app preferences (see below)

Running a command when Terminal start up

Terminal has plenty of scope for customisation and TotalTerminal adds a few more options Terminal > Preferences (CMD + , )

Before we head into what to type into the terminal its worth spending a little time on shortcuts. To make the most of the terminal we need to minimise the reliance on the mouse/trackpad.

Often you’ll need multiple terminals open, you may have a secure shell (ssh) open to a remote machine in one window and be running a build in another one, whatever the reason you don’t want to be having to reach for the mouse each time.

I prefer to have additional terminals opened as Tabbed panes, Command + T creates a new terminal tab, pressing it multiple times will create additional tabs. Once we have created these tabs we don’t want to have to fall back to using the mouse to navigate between them, fortunately the same shortcut for navigating between the tabbed panes in safari also works in Terminal/TotalTerminal

Command + Shift + } Advance forward a tab
Command + Shift + { Go backward a tab

For those who prefer a separate new window rather than a Tab, Command + N will create an instance of the Terminal in its own window. There’s still no need to resort to a mouse though, the following key combos will allow you to navigate between multiple windows from the same application, this works with multiple versions of most things including Xcode.

Command + ` Find the previous window opened
Command + Shift + ` Find the next window opened

We can open new Tabs and Windows, closing them in a mouse free world is just as simple

Command + w

This is the same keyboard shortcut that is used for closing an individual Tab in safari and a whole host of other applications so it’s well worth committing to muscle memory.

To close down the terminal completely press Command + q while Terminal/Totalterminal is the active application (i.e the application displayed in the top left next to the apple)

Closing down the intended app

Where are we?

To find the path to the directory you are currently within, use the (P)rint (W)orking (D)irectory command – pwd


Confused? When first getting used to the file hierarchy it may help to occasionally pop back into the Finder window from your current position, this is easily achieved using the open command;

>open .

Which will open the current directory in the finder window. From my home directory open . results in the following.

Open the current directory in Finder

Tip: For those who really want to keep their hands on the keyboard, we can close this finder window with Command + w, then jump back to the terminal by Using Command + Tab to select Terminal/TotalTerminal to be the foreground app again

How do I go somewhere different?

The (C)hange (D)irectory command – cd is used to navigate around the file hierarchy. By default in OSX, cd with no path argument will take you to the users home directory.


If you’re new to the command line it’s may be more useful to be able to find your location in Finder and then open this in the terminal. Not a problem, simply select the Folder in Finder and press Command + c to copy it into the clipboard. Now in a terminal window type ‘cd ‘ (the space is important) and then Command + v to paste the contents of the clipboard into the terminal and press return to navigate to the directory.

>cd /Volumes/SlowDrive/CODE <-- this path pasted from clipboard

Warning: If you select a file rather than a folder you will get a “Not a directory” error when you try and run this as you cannot change directory to a file.

To specify the users home directory explicitly you can use the tilda (~)

>cd ~
>echo ~

Here we’ve used the echo command to print out the value of the tilda.

Where can we go?

Before we spend any further time looking at how to go places it may help to know where we can go. The command to (L)i(S)t the contents of a directory is ls

The following example lists the content in the root directory of your Mac’s hard drive

>ls /
Applications Network Users bin dev home net sbin usr
Library System Volumes cores etc mach_kernel private tmp var

You can do a lot of damage by playing in the wrong part of the file system with the wrong command, so we’ll minimise the chances of causing chaos by keeping our experiments to a small corner of the users home directory.

First lets make sure we’re back in your home directory and then list the contents using ls.

>cd ~
Applications Desktop Documents Downloads Dropbox Library Movies Music Pictures Public Sites

The ls command without a path argument will simply list the contents of the folder you are currently in.

We can find out more information about these files/directories by using the -l flag which forces each entry onto a new line and provides additional information.

>ls -l
total 0
drwxr-xr-x 2 abitofcode staff 68 7 Dec 09:37 Applications
drwx——+ 22 abitofcode staff 748 1 Mar 16:12 Desktop
drwx——+ 18 abitofcode staff 612 16 Feb 23:30 Documents
drwx——+ 15 abitofcode staff 510 26 Feb 21:04 Downloads
drwx——@ 36 abitofcode staff 1224 28 Feb 20:46 Dropbox
drwx——@ 55 abitofcode staff 1870 17 Feb 00:26 Library
drwx——+ 14 abitofcode staff 476 23 Feb 12:27 Movies
drwx——+ 5 abitofcode staff 170 5 Dec 21:03 Music
drwx——+ 56 abitofcode staff 1904 22 Feb 17:18 Pictures
drwxr-xr-x+ 4 abitofcode staff 136 21 Aug 2013 Public
drwxr-xr-x+ 3 abitofcode staff 102 17 Feb 00:25 Sites

We can make those file sizes human readable and a little friendlier by adding the -h flag

>ls -lh
total 0
drwxr-xr-x 2 abitofcode staff 68B 7 Dec 09:37 Applications
drwx——+ 22 abitofcode staff 748B 1 Mar 16:12 Desktop
drwx——+ 18 abitofcode staff 612B 16 Feb 23:30 Documents
drwx——+ 15 abitofcode staff 510B 26 Feb 21:04 Downloads
drwx——@ 36 abitofcode staff 1.2K 28 Feb 20:46 Dropbox
drwx——@ 55 abitofcode staff 1.8K 17 Feb 00:26 Library
drwx——+ 14 abitofcode staff 476B 23 Feb 12:27 Movies
drwx——+ 5 abitofcode staff 170B 5 Dec 21:03 Music
drwx——+ 56 abitofcode staff 1.9K 22 Feb 17:18 Pictures
drwxr-xr-x+ 4 abitofcode staff 136B 21 Aug 2013 Public
drwxr-xr-x+ 3 abitofcode staff 102B 17 Feb 00:25 Sites

The ‘d’ character that starts each line indicates that the entry is a directory, every entry here is a directory.
All my work is under source control, typically git. I keep the working copies of my local repositories in a folder called REPOS within my Documents folder. As long as I push any changes I make to my local repositories back up to the repositories stored in ‘the cloud’ (bleurghh) then when my laptop is stolen/dropped/marinated in coffee I know all my work is safe.

To navigate to this folder I use the following

>cd ~/Documents/REPOS

This takes me to the REPOS folder in the Documents folder within my home directory (~), and to save typing this is the command I have run every time a new terminal is opened.

To create a directory named REPOS in your Documents folder you can either use Finder or the command line. Here we’ll use the command line;

>cd ~/Documents
>mkdir REPOS

Here we create the directory and then cd into it. Listing the contents reveals it’s empty


But adding the -a flag which lists entries that begin with a dot reveals the following.

>ls -la
total 0
drwxr-xr-x 2 abitofcode staff 68 1 Mar 16:51 .
drwx——+ 19 abitofcode staff 646 1 Mar 16:51 ..

Files that begin with a dot are hidden for a standard directory listing, however closer inspection here reveals that these are not files but directories. The single dot (.) means the ‘current directory’, the double dot (..) means the ‘parent directory’ and these can be used on their own or as part of any path passed to a command.

To go up from within the REPOS directory to the Documents folder we can simply type cd ..

>cd ..

These can be chained together, so to go up 2 directories we type cd ../..

Note: I’m going to avoid any destructive commands for removing files/directories at the moment as it’s very easy to do a lot of damage. If you want to remove the REPOS directory we have just created this can be done easily using Finder.

Tab completion

The Tab button offers path completion when working in the terminal, for instance if I was in my home directory and wanted to navigate to the Documents folder then I only need to type cd Doc and the remainder of the Documents path would be completed for me. This does assume that there were no other directories within my home folder that started with ‘Doc’

Navigating commands

Using the up and down arrow keys navigate through the history of commands you have already entered onto the command line. You can use these to find a command and run it again, or use the left and right arrow keys to navigate through the command to make any changes.

The clipboard

Occasionally you may want a list of the files in a directory. This can be easily achieved in a number of ways but in OSX there are 2 commands that make the clipboard accessible.

pbpaste will take what is in the clipboard and paste it into the terminal window

pbcopy can have other commands piped (|) into it so it’s then available for instance in Textmate or any other application that can access Command + v. The following puts a listing of the files in the current directory into the clipboard.

>ls | pbcopy

Warning: If you type and run pbcopy directly into the command line you may have trouble getting back to the standard cursor. This is because pbcopy used like this takes the stream of characters typed and places them in the clipboard until an end transmission character is received Control + d, alternatively you can terminate the command with Control + c

Built in help

To find out more on any of the commands discussed here use the ‘man’ command to read the manual pages on the specified command, use the up/down arrow keys to navigate the man page and the ‘q’ key to exit.

man cd

Using the man page

In part 2 we’ll clone a git repository so we have some folders and files to play with and introduce some more useful commands.

Command summary

Control + ` Open terminal via totalterminal
Command + t New tab
Command + Shift + } Next Tab
Command + Shift + { Previous Tab
Command + w Close window
Command + Q Close app

pwd (P)rint (W)orking (D)irectory
cd (C)hange (D)irectory
ls (L)i(s)t Directory
mkdir (M)a(K)e (Dir)ectory
pbpaste Paste Clipboard contents
pbcopy Receive Clipboard contents

Comments are closed.