Mac OS X is a Unix based operating system. That means that, under the smooth and eye candy Aqua user interface, lies an incredible power, which makes OS X such a marvelous system. In fact, Unix is the core of Mac OS X, aka the first level. Even if you don't know it, even if you don't see it, every thing you do on your Mac is powered by that Unix base. To see it, just open the Terminal application, found in /Applications/Utilities/.
Now don't be afraid... It's not really a user friendly interface, but remember it's just not supposed to be. The purpose of that program is to make a powerful interface between the Unix core and you. You got the whole system connected at your fingertips. Every command you will type will be interpreted, then executed by the system. You can practically do every thing with the terminal, even destroy your own machine, if you know the right commands. So keep in mind it's not a toy, and that you won't be warned. We're not on Microsoft OS, and we're really sure about this point...
This is my own terminal in action. Yours will probably looks different. Now, I will guide you into the basis of the Unix shell.
You will first notice a phrase saying «Welcome to Darwin». Darwin is the codename of the Mac OS X Unix foundation, based on BSD systems. Then, you got something like this:
cube:~ macmade$
That is called the prompt. It first says that you are on a machine called «cube» (that's my beloved one), that you are currently working in the «~» directory, which is the home of your user account (in /Users/). After that, there is the name of the user account running the terminal session. Finally, the dollar sign is here to tell you that you are using bash (Bourne Again Shell), the default OS X shell. Now you can practice typing basic commands. Try the following:
ls
Then hit the enter key. It will output something like this:
Desktop Documents Library Movies Music Pictures Public Sites
The command ls (list) is used to produce a list of all the files and directories in the current directory. Now try this:
ls -al /
This time, the output is quite different. With the ls command, we've passed two extra options (a and l) with an argument (/). Options are passed with the «-» sign, and can be combined. They determine how the command must react. In this example, we have told the machine to produce a detailed list (l), also including the hidden files (a). The argument / tells the command to list the root directory (your hard disk), instead of the directory we are working in. Every command can take options and arguments. Read the section about commands to find out more.
In a previous section of this tutorial, we have enabled the root user. Now we can gain total access to the system via the terminal. Try to type:
su
You'll be asked for the password you defined earlier. Notice that, after having entered the pass, the prompt has changed. Now you're connected as the root user. To return to your user's section, type «exit». Some of the commands we'll use will require a total access to the system. But you won't need to create each time a root session. You also can execute a single command as root from your session. Try the following:
sudo ls -al
The ls command has been executed with root privileges, but after that, you are still in a normal session. That's a more secure way to execute commands, since you gain total access just for the time necessary.
Here you will find some of the Unix commands we will use in this tutorial. You don't have to remember everything. It's just a little reference to help you understand what you will be doing. Please note that this section does not contain the full references of the commands. To obtain that, type «man» followed by the command name in the terminal.
1. pico
A friendly text editor. The default syntax is:
pico textfile
Use the keyboard's arrows to navigate through the document. Alternatively, you can use the [ctrl-y] and [ctrl-v] shortcuts to switch pages. Press [ctrl-x] to exit, [ctrl-o] to save, and [ctrl-w] to search for a word.
2. chown
Change Owner - Change the owner and the group associated with a file or a directory. The default syntax is:
chown [optional params] owner:group file
Option: | Name: | Description: |
|---|---|---|
-R | Recursive | Also change the owner and the group of all the files and folders contained in the specified directory. |
3. chmod
Change Mode - Change the permissions of a file or a directory. Each file has read, write and execute permissions for the owner, the group, and the rest of the world. So it's decomposed in a three digit sequence (owner – group – world). Then, each digit is a combination of the permissions values.
Read permission (R) - 4
Write permission (W) - 2
Execute permission (X) - 1
So if a file's mod is 753, as an example, it means that the owner has full access (R+W+X), the group has read and execute access (R+X), and the world write and execute access (W+X). The default syntax is:
chmod [optional params] mod file
Option: | Name: | Description: |
|---|---|---|
-R | Recursive | Also change the permissions of all the files and folders contained in the specified directory. |
4. ls
List - Output a list of the files found in the specified directory. The default syntax is:
ls [optional params] [optional directory]
Option: | Name: | Description: |
|---|---|---|
-a | All | Also display hidden files and directories. |
-c | Changed | Sort the files by the last modification date. |
-l | Long | Display a detailed view the files. |
-n | Numeric | Display the owner and group as numerical values. |
5. cd
Change directory - Go to the specified directory. The default syntax is:
cd directory
6. cp
Copy - Copy the specified file to the specified location. The default syntax is:
cp [optional params] file destination
Option: | Name: | Description: |
|---|---|---|
-R | Recursive | Copy the complete subtree, including the symbolic links. |
-f | Force | Overwrite any existing file without warning. |
-p | Preserve | Preserve all the file's properties, including modification time, owner, group, etc... |
-v | Verbose | Display all the files copied. |
7. mv
Move - Move a file to the specified location. The default syntax is:
mv [optional params] file destination
Option: | Name: | Description: |
|---|---|---|
-f | Force | Overwrite any existing file without warning. |
-v | Verbose | Display all the files moved. |
8. ln
Link - Create a link (an alias) to a file in the specified location. The default syntax is:
ln [optional params] file link
Option: | Name: | Description: |
|---|---|---|
-s | Symbolic | Create a symbolic link, instead of a hard link. |
9. mkdir
Make directory - The default syntax is:
mkdir [optional params] directory
Option: | Name: | Description: |
|---|---|---|
-m | Mod | Specify the chmod for the new directory. Must be followed by the chmod (eg. -m 777). |
10. rm
Remove - The default syntax is:
rm [optional params] file
Option: | Name: | Description: |
|---|---|---|
-d | Directory | Also delete directories. |
-f | Force | Remove the files without warning, regardless of the permissions. |
-R | Recursive | Delete all the file hierarchy (implies d option). |
-v | Verbose | Display all the files erased. |
11. Getting help
If you want to see quickly the options and the parameters available for a command, just type --help after the command.
12. Exiting the shell
When you're done with your set of commands, don't just close the terminal window. Enter «exit» until you see [Logout], to ensure a clean exit.
13. Escaping a command
Sometimes, a command may not respond. Just like normal applications, commands may result in an error. To avoid beeing blocked by the current process, you can type [ctrl-x] (control and x) to force quit the currently running command.