Basic OS X command line utilities and tips

OS X comes with a powerful command line interface as with any operating system derived from Unix. Before the age of graphical user interface, command line is the only way for human to interact with computer.

At the heart, command line is the place where you key in commands and programs, and the command line interface will run them for you. You can also combine commands into a script file and run them all at once.

To get access to the command line interface, run the Terminal app which is available at Applications > Utilities > Terminal. You might want to add the Terminal app to the dock. 

The prompt

When you first launched the Terminal app, it will open a command line window with a prompt similar to:

Last login: Mon Mar 24 11:15:38 on ttys001
$

It tells you when is your last login time, follow by a “$” prompt. The prompt is now waiting for you to enter command.

The command line prompt is customisable thus you might see a different symbol or text other than “$”. For example it is common to format the command line prompt to contain your computer name and user name.

machome:~ sandy$

In the above example, “machome” is the computer name and “sandy” is the user name. You will see a different prompt on your Mac depending on the setting of the prompt. We will describe how to customise the prompt in a future post. 

The shell

You can open one or more shell window under Terminal app, and they each work independently. As shown below, you can customise the color and font for each shell window under Terminal.

Basic OS X command line utilities and tips multiple windows

For each window that you open under Terminal, the app is running a shell program that handles the interaction with you. There are a number of shell programs available to serve as the command line interface. Popular choices include Bourne shell (sh), C Shell (csh), Korn Shell (ksh) and Bourne-Again shell (bash). Each shell program varies with each other by means of default command behaviour, settings files and syntax when writing shell scripts.

OS X comes with bash as default shell. You can off course change the shell according to your liking. We will be focusing on bash for this and future posts.

You can use the echo command to check the shell that your terminal is using:

$echo $SHELL
/bin/bash

For the above, we issue the echo command to print out the environment variable “$SHELL” which hold the value of the shell program.

Working directory

There is this concept of current working directory when you’re at a command line prompt. This is simply the directory you’re on when issuing a command at the prompt.

When you open a new terminal window, by default the shell program will land you at your home directory under /Users/.  Issue the pwd (print working directory) command to check your current working directory:

$pwd
/Users/sandy

Current directory is important as commands and tools will assume it is the working directory unless otherwise stated. This is especially so for commands that act on files on a directory.

Listing directory

You can list the content of the directory using ls:

$ ls
Applications
Desktop
Documents
Downloads
Library
Movies
Music
Pictures
Public

You can display more detail info using the “-l” parameter of ls:

$ ls -l
total 217952
drwx------ 3 sandy staff 102 Jan 19 01:26 Applications
drwxr-xr-x+ 20 sandy staff 680 Mar 24 09:28 Desktop
drwx------+ 10 sandy staff 340 Mar 19 12:45 Documents
drwx------+ 50 sandy staff 1700 Mar 24 09:07 Downloads
drwx------@ 74 sandy staff 2516 Jan 29 03:43 Library
drwx------+ 8 sandy staff 272 Mar 19 12:45 Movies
drwx------+ 6 sandy staff 204 Mar 19 12:45 Music
drwx------+ 11 sandy staff 374 Mar 19 12:45 Pictures
drwxr-xr-x+ 6 sandy staff 204 Mar 19 12:45 Public

The above shows how typically one uses parameters to change the behaviour of a command.

Changing directory

You change the current working directory by using the cd (Change Directory) command. To change to your Documents directory:

$ cd Documents
$ pwd
/Users/sandy/Documents

If you do not specified any parameter to cd, the shell will change the working directory to your home directory:

$ pwd
/Users/sandy/Documents
$ cd
$ pwd
/Users/sandy

Home directory is also represented by the symbol “~”:

$ cd ~/Desktop
$ pwd
/Users/sandy/Desktop
$ cd ~
$ pwd
/Users/sandy

 To go back one directory level up, use “..”:

$ cd ~/Pictures
$ pwd
/Users/sandy/Pictures
$ cd ..
$ pwd
/Users/sandy
$ cd Music
$ pwd
/Users/sandy/Music
$ cd ../Applications
$ pwd
/Users/sandy/Applications 

Get help with man

To get the help page for a command, use the man (Manual Page) command. The help page is also called a man page. Thus to display the man page for ls command:

$ man ls

Basic OS X command line utilities and tips manual

Use the arrow key or space bar to scroll the manual page. Press the “q” key to quit.

Auto Completion

When you’re entering command line parameters, use the tab key to auto complete directory and file name. Type the first few characters, and then press the tab key, and the command line will fill the rest for you.

For example when at you home directory, and to navigate to the Desktop folder, enter “cd De” and press the tab key. The shell will fill up the command line with “cd Desktop” for you.

Warnings

Please note that command line interface gives you all the power to control your computer, up to the point of total destruction. Thus it is important to understand a command before you run it.