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 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 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.
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).
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):
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 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.
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:
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).
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.
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:
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.
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.
Format: alias shortcut="command_or_directory"
Format: cat a_file_name
Format: cd directory (can be absolute or relative)
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
Modifiers:
Example: chmod o-w a_file_name (would stop [-] others [o] from modifying [w] 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)
Modifiers:
Format: exit
Format: find directory_to_look_in
Modifiers:
Example: sudo find / -name httpd.conf would search your entire computer for the Apache config file httpd.conf (in this case, sudo skips errors)
Don't waste your time on this, instead get a great client like Cyberduck
Format: gzip a_file
Modifiers:
Note: use gunzip to unzip a gzipped file
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
Format: less a_file_name
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/django-admin.py ~/myproject/django-admin.py
Format: ls modifiers
Select Modifiers:
Format: man command_name (from man, hit q to return to command line)
Example: man cd
Format: mkdir a_directory_name
Example: mkdir Work\ Files (creates a directory called "Work Files")
Format: same as cp
Format: pwd
Format: ps
Modifiers:
Example: ps - ax (to show all processes running on your Mac)
Format: rm a_file
Modifiers
Note: There's no "undo" if you delete a file
Note: You can delete all the files in a directory by typing rm *
Format: rmdir a_directory
Note: You have to delete all the files in the directory first
Format: split -b 900000K iPhoto_Library.zip
Note: This example would split the file "iPhoto_Library.zip" into multiple 900 Mb files
Note: To recombine, you would use cat file1 file2 file3 > iPhoto_Library.zip
Format: ssh username@IP_address
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
Format: tar file_or_directory_to_archive/a_file_to_expand_ending_in.tz
Modifiers:
Example: tar -xvfz mysql.tar.gz would unzip and unarchive the file/directory mysql