An introduction to UNIX on the Mac

Why would you possibly want to learn UNIX on the Mac? Maybe you want to get under the hood of your Mac to know how it works. Maybe you want to install some software like MySQL. Or maybe you want to get a server up and running so you need to learn about the command line.

What follows is a brief intro. It's not exhaustive and is aimed at people without a Computer Science degree who want to learn more and get to the point where they can ask smart questions. For further reading, I suggest checking out Learning Unix for Mac OS X Tiger. It's a little out of date as the switch to Leopard changed how some commands are handled, but it's excellent reading - and written by a true expert.

UNIX and the Mac OS

UNIX is an operating system that dates back to 1969 when it was created by folks at Bell Labs. When you use UNIX you're basically getting 40 years of continuous improvement under the hood, meaning that you're using one of the most powerful and stable operating systems around.

The Mac OS X is an operating system that is build on top of UNIX (prior to OS X, Macs didn't use UNIX). It takes the raw power and stability of UNIX and adds a beautiful graphical interface called Aqua on top.

This is an important thing to understand: if you want to really use UNIX on your Mac, you're going to have to get below the graphical interface and use the command line. People have written graphical user interfaces to run on top of UNIX (Apple wrote Aqua; X11 is another popular one), but to get the full power, you need to use the command line.

The Command Line and the Mac OS

The command line is accessed on your Mac via a program called Terminal that is found via Applications::utilities::Terminal. Double-click it to open and you should see an image like the one below.

Terminal Window

UNIX Concepts and Lingo

Before diving into the guts of UNIX, let's talk briefly about different concepts in UNIX and how to relate them to what you know in the Mac OS

On your Mac, you're used to thinking in terms of storing things (files and applications) in folders or the desktop and then using commands within an application to do things (e.g., create a folder in the Finder, etc.).

UNIX abstracts this a bit. The universe now consists of directories, each of which can contain files, programs or other directories. Whenever you want to do anything, you use a program that you execute via the command line.

Want to copy a file? On your Mac you'd simply choose the Duplicate command. In UNIX you run the cp program. Conceptually, just remember that in UNIX there's a specific program you call to do anything.

Let's talk a bit more about those directories. First, they're all hierarchical and they roll up to a master directory denoted as root. In UNIX, everything is measured relative to this root directory (examples are forthcoming).

Each user on a system has a home directory. This is also the directory where you start at when you log into UNIX. On my Mac, my user name is lindsayrgwatt. When I boot up my computer, there's a place called lindsayrgwatt that looks like the image below (Note that my hard drive is called The Whirr Box and I've an iDisk; you may not. Texts & Papers is also my own creation).

lindsayrgwatt directory

Now take a look back at that first image showing the Terminal. bear with me for a second as I explain how this relates to the image above.

Notice how in the first image it says lindsayrgwatt? That's because that's what my user name is.

Notice as well how it says lindsay-watts-macbook-pro? That's the name of my computer and shows me what system I'm running on (yours will say something different. If you were to access a remote server, this would change yet again).

So now there are only two things we don't understand: the ~ and the $. The ~ is a UNIX shortcut to refer to your home directory. For me, that's the lindsayrgwatt directory I put up a photo of above. To check this, I could type ls at the command line in the terminal, hit return and I'd see the following (Note that you always have to hit the return key to execute a command in the Terminal):

lindsayrgwatt directory from terminal

Notice that this is exactly the same list of files as shown in the prior image illustrating what my directory looked like in the Finder.

So we have one last mystery: what is the $? That's just a character showing you where you can enter text on the command line. You can actually change it to be whatever you want.

Now that you're warming up, a couple more concepts. Terminal is actually a program called a shell that lets you enter commands. In fact, there are multiple flavours of shells and the default is one called bash. You can change it, but most people won't need to.

Also, each program you run launches a process. Since UNIX is capable of multitasking, you can have multiple processes running at once. In fact, you always have multiple processes running as you'll see later.

Finally, most programs have modifiers. Modifiers cause the same program to run, but with a slightly different flavour. For instance, ls above listed what was in a directory. Typing ls -ax (note the space) causes all the files and directories within lindsayrgwatt to be shown. Try it and you'll find there are a lot of files that are normally hidden when you look at a folder in the Finder.

A Couple More Things

A few more things you should be aware of. Inside terminal, you use the control key instead of the command key to manage a bunch of shortcuts. Typing control + c will cancel whatever program (a.k.a. a process) you are currently running (or do nothing if there's no program currently running).

UNIX is case and character sensitive. If you type something wrong-even one character-the right thing will not happen (and you'll get some truly arcane feedback). As a result, watch what you type. A tip: you can type the first few letters of anything inside the current directory and hit the Tab key to have Terminal complete it. If you want to go back to an old command you typed, just use the up arrow (and the down arrow if you overshoot).

Also, spaces needs to be escaped. The Whirr Box must be written as The\ Whirr\ Box. If you use that Tab trick from above, Terminal will escape stuff for you.

If you get tired of writing really long character strings, you can create an alias. To change to my directory that was all the files on my desktop in the finder (that's right - your desktop is just a directory that's rendered by Aqua in a special way), I could type cd ~/Desktop. This gets pretty dull pretty quick, so instead I could write alias desktop="cd ~/Desktop". Now every time I type desktop, that program will run.

A few last shortcuts. .. refers to the directory that is one level above the current one. . refers to the current directory. / refers to the root directory. If you want to see all the UNIX programs available to you, hit the Tab key twice from the command line in terminal. You'll see over a thousand different programs.

Playful Exploration

So now that you've drunk from the firehose of UNIX terminology and concepts, let's play around - but in a way that can't harm our system (UNIX is very powerful and there's no undo, so if you screw up, you can really damage your system).

First action. Let's get to root. In the Terminal, type in cd / and hit return. cd is short for change directory and takes a directory path as a variable. Now try typing ls (ls is short for list). Here's what I see:

Snapshot of Root

This is what's in your root directory. The items containing mach refer to the underpinnings of the Mac OS (For more on kernels, see here). The directories like bin, dev, usr, etc. are standard to all UNIX operating systems and contain UNIX system files. If you looked at Linux or a different system, you'd see a similar structure. If you install a program like MySQL, it will not show up in the Finder, rather you find in via the command line in the /bin directory.

Let's go over to the Volumes directory and see what's there. You'll notice that all it contains (at least on my Mac) is my hard drive, the whirr box. If I go to the whirr box directory, I see that it lists the same as root. In other words, for my mac, the whirr box points to root. Check out the image below to see the commands I used to get there. It may take a couple of minutes for this to sink in as it's a little screwy at first.

Note also that I have not typed the full path of each directory (e.g., /Volumes/The\ Whirr\ Box), but used relative pathways (measured relative to the current directory that I am in).

Whirr Box File List

Now try typing ls -a (list plus the a [for all] modifier) to see if there are any hidden files. On mine there are a ton as you can see in the image below.

Whirr Box All Files

Now you know how to move around directories and list files. How about seeing what's running on your computer?

Try typing in ps (short for process). Not too sexy; there doesn't appear to be too much running. Lets try running it with the -ax (for all processes including ones that are external to what's going on in Terminal) modifiers. here's what I get:

List of processes running on my mac

You could go blind looking at this image, so here's a quick summary of what you're seeing. These are all the processes running on my mac. Most of them are system processes that keep my Mac functioning. Some are applications that I could see in the finder (for instance, Preview, Safari, etc.). Those processes are denote by a path of the form /Applications/ApplicationName... . note also the number associated with the process (the Pid); these are unique numbers given to each process by the UNIX operating system.

Now you can leave the Terminal by typing exit. Always leave by typing exit or you risk having some processes continuing unnecessarily.

What's Next?

Now you know a bit about UNIX on the Mac. There's lots you can do (the book listed above opens up a world to you). I'm not going to walk you through more, rather I'm going to list a bunch of what I consider to be the most interesting yet approachable UNIX commands. What follows is a brief (definitely not exhaustive) description of each. Have fun exploring the power of UNIX on your Mac.

alias - create an alias for a command or directory

Format: alias shortcut="command_or_directory"

cat - show the contents of a text file on screen (concatenate it)

Format: cat a_file_name

cd - change directory

Format: cd directory (can be absolute or relative)

chmod - change/modify permissions for a file

Description: files have read/write/execute permission. You can add/delete/specify exactly these permissions for the file's owner/a group/someone else or everyone

Format: chmod permission file


Example: chmod o-w a_file_name (would stop [-] others [o] from modifying [w] a file)

cp - copy a file

Format: cp old_file_including_pathway new_file_including_pathway

Format: cp old_file_including_pathway new_directory_pathway (copies a file to an existing directory)


exit - quit the shell

Format: exit

find - search for a file or directory name

Format: find directory_to_look_in


Example: sudo find / -name httpd.conf would search your entire computer for the Apache config file httpd.conf (in this case, sudo skips errors)

ftp - transfer files to/from a remote system

Don't waste your time on this, instead get a great client like Cyberduck

gzip - compress a file

Format: gzip a_file


Note: use gunzip to unzip a gzipped file

kill - stop a process

Format: kill a_process_number

Note: be careful killing processes. If you kill a necessary system process you might find your Mac is unstable and you have to restart

less - show the contents of a text file on screen one page at a time

Format: less a_file_name

ln - create a link (alias)

format: ln -s path/to/original/file.type path/to/alias.type

Comment: Really useful when trying to work with programming libraries ln -s /Library/Python/2.5/site-packages/django/bin/ ~/myproject/

ls - list the contents of a directory

Format: ls modifiers

Select Modifiers:

man - shows the help (manual) for a given program

Format: man command_name (from man, hit q to return to command line)

Example: man cd

mkdir - create (make) a directory (folder)

Format: mkdir a_directory_name

Example: mkdir Work\ Files (creates a directory called "Work Files")

mv - move a file

Format: same as cp

pwd - shows the present working directory

Format: pwd

ps - show processes currently running

Format: ps


Example: ps - ax (to show all processes running on your Mac)

rm - remove (delete) a file

Format: rm a_file


Note: There's no "undo" if you delete a file

Note: You can delete all the files in a directory by typing rm *

rmdir - remove (delete) a directory (folder)

Format: rmdir a_directory

Note: You have to delete all the files in the directory first

split - split a file into multiple parts

Format: split -b 900000K

Note: This example would split the file "" into multiple 900 Mb files

Note: To recombine, you would use cat file1 file2 file3 >

ssh - remote login to a secure shell

Format: ssh username@IP_address

sudo - execute a command as an administrator (superuser do)

Format: sudo a_normal_command

Note: You'll be asked for your password. Also be aware that as superuser, many of the checks put in place to stop you from accidentally deleting things will be turned off. You'll typically only use sudo when you're installing new software

tar - archive a few files/expand a backup (tape archive)

Format: tar file_or_directory_to_archive/


Example: tar -xvfz mysql.tar.gz would unzip and unarchive the file/directory mysql