When you start the Terminal application, you are looking at a program called a shell. The shell is a program that looks like the traditional model of a window into which you type commands and see their output. The major transition from Mac OS 9 to OS X was the use of a version of the Unix operating system instead of the traditional Mac OS. A shell program has been the traditional text-based mode of interaction with Unix from its beginning, and the Terminal is running a shell.
Different versions of shell programs have been created since programmers started running Unix in the 1970s. The shell that runs by default in Mac OS X is called the Bash shell. All shell versions have a core set of commands. A few commands will get you started, but we'll also need to talk about the ideas behind some of them.
The file system has a base directory, called the "root", and is represented with a single forward slash character (/). Any file in that directory can be specified by preceding its name with the slash, for example, /usr. A directory in another directory uses the slash character to separate the names. For example, /usr/local/src means the file named "src" in the directory called "local" which is itself in the directory called "usr". Just from this filename, we can't tell if "src" is a directory or not, though /usr/local/src/ (with the trailing slash) would make it clear. A directory is just a special kind of file, so the way you construct filenames is the same both for regular files and directories.
man man
Since Unix comes from the ancient times of slow teletypes, shell
commands are typically very short, but are usually mnemonic for the
activity that they perform.
ls /
The ls command has a number of options that modified what it
lists. For example, to see a "long listing" (including dates and the
size of the files), use the -l option:
ls -l /
To see the documentation of ls, you can use the man command:
man ls
By default, ls will list the contents of the current
directory
if you don't specify the name of a file to list.
One very handy set of options lists the files in reverse chronological order, so the last file listed is the most recent that was created or modified:
ls -ltr
This is very useful when you are trying to figure out which file you
modified last.
Your home directory has a special name: the tilde character (~). (Remember the value of brevity for anyone using a teletype.) You can use the tilde as a directory name. To see the size of a file called "notes" in your home directory, for example, you would enter:
ls -l ~/notes
If you want to refer to someone else's home directory, you follow the
tilde character with their user name. Since my user name is "ack" (my
initials), then you could use ls with my notes file as an argument like this:
ls -l ~ack/notes
If you try it, you'll get an error (since there isn't a file named
"notes"), but you'll also see how the shell has turned "~" into a
complete pathname for my home directory.
The tilde character is also used in web addresses. For example, my
webpage at CalArts was http://im.calarts.edu/~ack — my username
was "ack", so "~ack" meant my home directory.
pwd
You can refer to files in the current directory without including
the directory name; its assumed that the file is in the current
directory. This means that when you are editing files in a directory
(one of your programs, for example), it's handy to make that directory
your current directory so you can easily refer to files in that
directory.
For example, if your home directory is the current directory, then you could see a long listing of the file "notes" like this:
ls -l notes
You refer to the current directory and the directory that contains it so frequently they have shortcut names. The current directory can be referred to with the period character (.). Because ls without arguments refers the current directory, the following commands are eqiuvalent:
ls
ls .
The directory "above" the current directory — its parent — has the
shortcut name of two period characters (..). If you want to see the
list of directories at the same level in the hierarchy as your current
directory, then can list its contents like this:
ls ..
(In the Unix shell at least, brevity is still the soul of wit.)
cd ~/project1
Without arguments, cd will change the current directory to
your home directory. Using this, you could change to the "project1"
directory with these two commands:
cd
cd project1
cat blue.py
This name doesn't seem to make much sense until you know that you can
chain multiple files together in this way, and "catenated" means
"connected in a chain."
cat blue.py black.py circle.py
cp notes notes.backup
In Unix, there are few requirements on the structure of filenames,
so I've added ".backup" to my filename as a personal convention I'll
use when I make a backup copy of a file.
mkdir ~/pdm
The rules for directory names is the same as for regular files, so if
you don't specify the directory in which you want to create the new
directory, the current directory will be used. For example, if you
first make your home directory your current directory, you can specify
the directory name alone to mkdir.
cd
mkdir pdm
mv notes notes1
There is now no longer a file called "notes", but a new file called
"notes1" that is a perfect duplicate of file "notes". (Why "mv"
instead of "rename" or "rn"? Probably because "rn" is too close to
the way we delete files, "rm".)
rm notes.old
rmdir current_notes
Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The ">>>" symbol is a prompt for you to type Python code, just
like in a Unix shell. You can experiment with simple Python
expressions this way. However, a Python program of any size at all
will be saved in a file, usually with a filename that ends with ".py".
You can run this file as if you typed it into Python by using the
filename as an argument. For example, if I had a Python file called
"project1.py", I could run it by entering this command to the Unix
shell:
python project1.py
We'll talk about supplying arguments to our Python scripts in the Python script tutorial
next week.
| man command | Show the manual page for command. |
| ls | List the files in the current directory. |
| ls directory | List the files in directory. |
| ls -l directory | List the files in directory with extra information. |
| ls -ltr directory | List the files in directory with the newest files last. |
| cat filename(s) | Output the file or files to the terminal window. |
| pwd | Print the name of the current directory. |
| cd | Change the current directory to your home directory. |
| cd directory | Change the current directory to directory. |
| cp existing-file new-file | Copy existing-file to new-file. The original file still exists. |
| mv existing-file new-file | Rename existing-file to new-file. The original file no longer exists. |
| rm filename | Delete file filename. |
| rmdir directory | Delete directory directory. The directory must be empty. |
| rm -r directory | Delete directory directory including everything it contains. Be careful! |
open tutorial.html
You can open the Finder in this way. Since the current directory is
called ".", you can create a Finder window for the current directory with:
open .
When we make image files, the open command will by default load
that image into Preview if the type of the file is one that Preview
can interpret.
Another Unix and shell tutorial goes into more detail. It describes how to make terminal windows in Linux, but the principle is the same. At the bottom of the first page is a link to a description of a history of Unix.